Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
one
提交
b35edce4
O
one
项目概览
OpenDocCN
/
one
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
one
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b35edce4
编写于
3月 20, 2019
作者:
lzc828
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
模型多态
上级
6b014a98
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
146 addition
and
10 deletion
+146
-10
src/Database/Mysql/MorphOne.php
src/Database/Mysql/MorphOne.php
+113
-0
src/Database/Mysql/RelationTrait.php
src/Database/Mysql/RelationTrait.php
+33
-10
未找到文件。
src/Database/Mysql/MorphOne.php
0 → 100644
浏览文件 @
b35edce4
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019/3/20
* Time: 17:37
*/
namespace
One\Database\Mysql
;
class
MorphOne
{
protected
$remote_type
=
[];
protected
$remote_type_id
=
[];
/**
* @var Model
*/
protected
$model
;
/**
* @var ListModel
*/
protected
$list_model
;
protected
$self_id
=
''
;
protected
$self_type
=
''
;
public
function
__construct
(
$remote_type
,
$remote_type_id
,
$self_type
,
$self_id
,
$model
)
{
$this
->
self_id
=
$self_id
;
foreach
(
$remote_type
as
$type
=>
$remote_model
)
{
$this
->
remote_type
[
$type
]
=
new
$remote_model
(
$this
);
}
$this
->
remote_type_id
=
$remote_type_id
;
$this
->
model
=
$model
;
$this
->
self_type
=
$self_type
;
}
/**
* @param array $calls [\Closure(Model)]
* @return $this
*/
public
function
each
(
array
$calls
)
{
foreach
(
$calls
as
$type
=>
$call
)
{
if
(
isset
(
$this
->
remote_type
[
$type
]))
{
$call
->
call
(
$this
->
remote_type
[
$type
]);
}
}
return
$this
;
}
public
function
setRelation
()
{
$type
=
$this
->
model
[
$this
->
self_type
];
foreach
(
$this
->
remote_type
as
$type1
=>
$remote_model
)
{
if
(
$type1
==
$type
)
{
$remote_model
->
where
(
$this
->
remote_type_id
[
$type
],
$this
->
model
[
$this
->
self_id
]);
}
else
{
unset
(
$this
->
remote_type
[
$type1
]);
}
}
return
$this
;
}
public
function
setRelationList
(
ListModel
$list_model
)
{
$this
->
list_model
=
$list_model
;
$types
=
$this
->
list_model
->
pluck
(
$this
->
self_type
,
true
,
true
);
foreach
(
$this
->
remote_type
as
$type
=>
$remote_model
)
{
if
(
isset
(
$types
[
$type
]))
{
$ids
=
array_column
(
$types
[
$type
],
$this
->
self_id
);
$remote_model
->
whereIn
(
$this
->
remote_type_id
[
$type
],
$ids
);
}
else
{
unset
(
$this
->
remote_type
[
$type
]);
}
}
return
$this
;
}
public
function
setRelationModel
(
Model
$model
)
{
$this
->
model
=
$model
;
return
$this
->
setRelation
();
}
public
function
get
()
{
return
end
(
$this
->
remote_type
)
->
find
();
}
public
function
merge
(
$key
)
{
if
(
$this
->
list_model
===
null
)
{
$this
->
model
->
$key
=
$this
->
get
();
}
else
{
$third_arr
=
$this
->
third_model
->
findAll
()
->
pluck
(
$this
->
third_column
,
true
);
foreach
(
$this
->
list_model
as
$val
)
{
$k
=
$val
[
$this
->
self_column
];
$val
->
$key
=
isset
(
$third_arr
[
$k
])
?
$third_arr
[
$k
]
:
null
;
}
}
unset
(
$this
->
model
,
$this
->
third_model
);
}
}
\ No newline at end of file
src/Database/Mysql/RelationTrait.php
浏览文件 @
b35edce4
...
...
@@ -6,25 +6,48 @@ namespace One\Database\Mysql;
trait
RelationTrait
{
/**
* @param $self_column
* @param
$third
* @param
$third
_column
* @param
string
$self_column
* @param
string $remote
* @param
string $remote
_column
* @return Model
*/
protected
function
hasOne
(
$self_column
,
$
third
,
$third
_column
)
protected
function
hasOne
(
$self_column
,
$
remote
,
$remote
_column
)
{
return
new
HasOne
(
$self_column
,
$
third
,
$third
_column
,
$this
);
return
new
HasOne
(
$self_column
,
$
remote
,
$remote
_column
,
$this
);
}
/**
* @param $self_column
* @param
$third
* @param
$third
_column
* @param
string
$self_column
* @param
string $remote
* @param
string $remote
_column
* @return Model
*/
protected
function
hasMany
(
$self_column
,
$
third
,
$third
_column
)
protected
function
hasMany
(
$self_column
,
$
remote
,
$remote
_column
)
{
return
new
HasMany
(
$self_column
,
$
third
,
$third
_column
,
$this
);
return
new
HasMany
(
$self_column
,
$
remote
,
$remote
_column
,
$this
);
}
/**
* @param array $remote_type [$self_type => $remote_model_class]
* @param array $remote_type_id [$self_type => $remote_table_rel_id]
* @param string $self_id
* @return MorphOne
*/
protected
function
morphOne
(
array
$remote_type
,
array
$remote_type_id
,
$self_id
)
{
return
new
MorphOne
(
$remote_type
,
$remote_type_id
,
$self_id
,
$this
);
}
/**
* @param array $remote_type [$self_type => $remote_model_class]
* @param array $remote_type_id [$self_type => $remote_table_rel_id]
* @param string $self_id
* @return Model
*/
protected
function
morphMany
(
array
$remote_type
,
array
$remote_type_id
,
$self_id
)
{
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录