模型的hasWhere方法增加fields参数

This commit is contained in:
thinkphp
2017-12-01 11:43:18 +08:00
parent dc62af3494
commit 88599fe33d
9 changed files with 44 additions and 25 deletions

View File

@@ -1782,11 +1782,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @access public * @access public
* @param string $relation 关联方法名 * @param string $relation 关联方法名
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Relation|Query * @return Relation|Query
*/ */
public static function hasWhere($relation, $where = []) public static function hasWhere($relation, $where = [], $fields = null)
{ {
return (new static())->$relation()->hasWhere($where); return (new static())->$relation()->hasWhere($where, $fields);
} }
/** /**

View File

@@ -80,13 +80,15 @@ class BelongsTo extends OneToOne
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
$table = $this->query->getTable(); $table = $this->query->getTable();
$model = basename(str_replace('\\', '/', get_class($this->parent))); $model = basename(str_replace('\\', '/', get_class($this->parent)));
$relation = basename(str_replace('\\', '/', $this->model)); $relation = basename(str_replace('\\', '/', $this->model));
if (is_array($where)) { if (is_array($where)) {
foreach ($where as $key => $val) { foreach ($where as $key => $val) {
if (false === strpos($key, '.')) { if (false === strpos($key, '.')) {
@@ -95,8 +97,10 @@ class BelongsTo extends OneToOne
} }
} }
} }
$fields = $this->getRelationQueryFields($fields, $model);
return $this->parent->db()->alias($model) return $this->parent->db()->alias($model)
->field($model . '.*') ->field($fields)
->group($model . '.' . $this->foreignKey) ->group($model . '.' . $this->foreignKey)
->join($table . ' ' . $relation, $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType) ->join($table . ' ' . $relation, $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType)
->where($where); ->where($where);
@@ -125,7 +129,7 @@ class BelongsTo extends OneToOne
} }
if (!empty($range)) { if (!empty($range)) {
$data = $this->eagerlyWhere($this, [ $data = $this->eagerlyWhere($this->query, [
$localKey => [ $localKey => [
'in', 'in',
$range, $range,
@@ -168,7 +172,7 @@ class BelongsTo extends OneToOne
{ {
$localKey = $this->localKey; $localKey = $this->localKey;
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
$data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); $data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure);
// 关联模型 // 关联模型
if (!isset($data[$result->$foreignKey])) { if (!isset($data[$result->$foreignKey])) {
$relationModel = null; $relationModel = null;

View File

@@ -213,10 +213,11 @@ class BelongsToMany extends Relation
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
* @throws Exception * @throws Exception
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
throw new Exception('relation not support: hasWhere'); throw new Exception('relation not support: hasWhere');
} }

View File

@@ -254,13 +254,15 @@ class HasMany extends Relation
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
$table = $this->query->getTable(); $table = $this->query->getTable();
$model = basename(str_replace('\\', '/', get_class($this->parent))); $model = basename(str_replace('\\', '/', get_class($this->parent)));
$relation = basename(str_replace('\\', '/', $this->model)); $relation = basename(str_replace('\\', '/', $this->model));
if (is_array($where)) { if (is_array($where)) {
foreach ($where as $key => $val) { foreach ($where as $key => $val) {
if (false === strpos($key, '.')) { if (false === strpos($key, '.')) {
@@ -269,8 +271,11 @@ class HasMany extends Relation
} }
} }
} }
$fields = $this->getRelationQueryFields($fields, $model);
return $this->parent->db()->alias($model) return $this->parent->db()->alias($model)
->field($model . '.*') ->field($fields)
->group($model . '.' . $this->localKey) ->group($model . '.' . $this->localKey)
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey) ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey)
->where($where); ->where($where);

View File

@@ -78,9 +78,10 @@ class HasManyThrough extends Relation
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
throw new Exception('relation not support: hasWhere'); throw new Exception('relation not support: hasWhere');
} }

View File

@@ -82,13 +82,15 @@ class HasOne extends OneToOne
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
$table = $this->query->getTable(); $table = $this->query->getTable();
$model = basename(str_replace('\\', '/', get_class($this->parent))); $model = basename(str_replace('\\', '/', get_class($this->parent)));
$relation = basename(str_replace('\\', '/', $this->model)); $relation = basename(str_replace('\\', '/', $this->model));
if (is_array($where)) { if (is_array($where)) {
foreach ($where as $key => $val) { foreach ($where as $key => $val) {
if (false === strpos($key, '.')) { if (false === strpos($key, '.')) {
@@ -97,8 +99,10 @@ class HasOne extends OneToOne
} }
} }
} }
$fields = $this->getRelationQueryFields($fields, $model);
return $this->parent->db()->alias($model) return $this->parent->db()->alias($model)
->field($model . '.*') ->field($fields)
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType) ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType)
->where($where); ->where($where);
} }

View File

@@ -83,9 +83,10 @@ class MorphMany extends Relation
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
throw new Exception('relation not support: hasWhere'); throw new Exception('relation not support: hasWhere');
} }

View File

@@ -82,9 +82,10 @@ class MorphOne extends Relation
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
throw new Exception('relation not support: hasWhere'); throw new Exception('relation not support: hasWhere');
} }

View File

@@ -83,9 +83,10 @@ class MorphTo extends Relation
* 根据关联条件查询当前模型 * 根据关联条件查询当前模型
* @access public * @access public
* @param mixed $where 查询条件(数组或者闭包) * @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query * @return Query
*/ */
public function hasWhere($where = []) public function hasWhere($where = [], $fields = null)
{ {
throw new Exception('relation not support: hasWhere'); throw new Exception('relation not support: hasWhere');
} }