提交 b35edce4 编写于 作者: lzc828's avatar lzc828

模型多态

上级 6b014a98
<?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
......@@ -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.
先完成此消息的编辑!
想要评论请 注册