改进自关联查询多级调用问题

This commit is contained in:
thinkphp
2018-01-05 17:28:21 +08:00
parent 1f06d05cbe
commit ea2ce8f8de
4 changed files with 34 additions and 23 deletions

View File

@@ -192,7 +192,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$con = Db::connect($connection);
// 设置当前模型 确保查询返回模型对象
$queryClass = $this->query ?: $con->getConfig('query');
$query = new $queryClass($con, $this->class);
$query = new $queryClass($con, $this);
// 设置当前数据表和模型名
if (!empty($this->table)) {
@@ -208,6 +208,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $query;
}
/**
* 创建新的模型实例
* @access public
* @param array|object $data 数据
* @param bool $isUpdate 是否为更新
* @param mixed $where 更新条件
* @return Model
*/
public function newInstance($data = [], $isUpdate = false, $where = null)
{
return (new static($data))->isUpdate($isUpdate, $where);
}
/**
* 获取当前模型的查询对象
* @access public
@@ -599,7 +612,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
protected function getRelationData(Relation $modelRelation)
{
if ($this->parent && get_class($this->parent) == $modelRelation->getModel()) {
if ($this->parent && $modelRelation->getModel() == $this->parent) {
$value = $this->parent;
} else {
// 首先获取关联数据