mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进HasMany和OneToOne
This commit is contained in:
@@ -245,7 +245,7 @@ class HasMany extends Relation
|
||||
}
|
||||
return $this->parent->db()->alias($model)
|
||||
->field($model . '.*')
|
||||
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType)
|
||||
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey)
|
||||
->where($where);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,24 +54,25 @@ class HasOne extends OneToOne
|
||||
/**
|
||||
* 根据关联条件查询当前模型
|
||||
* @access public
|
||||
* @param Model $model 模型对象
|
||||
* @param mixed $where 查询条件(数组或者闭包)
|
||||
* @return Query
|
||||
*/
|
||||
public function hasWhere($model, $where = [])
|
||||
public function hasWhere($where = [])
|
||||
{
|
||||
$table = $this->query->getTable();
|
||||
$table = $this->query->getTable();
|
||||
$model = basename(str_replace('\\', '/', get_class($this->parent)));
|
||||
$relation = basename(str_replace('\\', '/', $this->model));
|
||||
if (is_array($where)) {
|
||||
foreach ($where as $key => $val) {
|
||||
if (false === strpos($key, '.')) {
|
||||
$where['b.' . $key] = $val;
|
||||
$where[$relation . '.' . $key] = $val;
|
||||
unset($where[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $model->db()->alias('a')
|
||||
->field('a.*')
|
||||
->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType)
|
||||
return $this->parent->db()->alias($model)
|
||||
->field($model . '.*')
|
||||
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType)
|
||||
->where($where);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,18 @@ abstract class OneToOne extends Relation
|
||||
// 要绑定的属性
|
||||
protected $bindAttr = [];
|
||||
|
||||
/**
|
||||
* 设置join类型
|
||||
* @access public
|
||||
* @param string $type JOIN类型
|
||||
* @return $this
|
||||
*/
|
||||
public function joinType($type)
|
||||
{
|
||||
$this->joinType = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 预载入关联查询(JOIN方式)
|
||||
* @access public
|
||||
|
||||
Reference in New Issue
Block a user