增加Pivot对象parent属性

This commit is contained in:
yunwuxin
2017-03-02 16:06:37 +08:00
parent 6d2541585d
commit 0952e9231f
2 changed files with 10 additions and 4 deletions

View File

@@ -16,13 +16,17 @@ use think\Model;
class Pivot extends Model
{
/** @var Model */
public $parent;
/**
* 构造函数
* @access public
* @param array|object $data 数据
* @param string $table 中间数据表名
* @param Model $parent
* @param array|object $data 数据
* @param string $table 中间数据表名
*/
public function __construct($data = [], $table = '')
public function __construct(Model $parent, $data = [], $table = '')
{
if (is_object($data)) {
$this->data = get_object_vars($data);
@@ -30,6 +34,8 @@ class Pivot extends Model
$this->data = $data;
}
$this->parent = $parent;
$this->table = $table;
}

View File

@@ -64,7 +64,7 @@ class BelongsToMany extends Relation
protected function newPivot($data)
{
$pivot = $this->pivot ?: '\\think\\model\\Pivot';
return new $pivot($data, $this->middle);
return new $pivot($this->parent, $data, $this->middle);
}
/**