diff --git a/.gitignore b/.gitignore index 723ef36f..17a3431d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ +/composer.lock +/vendor .idea \ No newline at end of file diff --git a/library/think/Model.php b/library/think/Model.php index d140d839..f0c693a4 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -598,7 +598,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected static function parseQuery(&$data, $with, $cache) { $result = self::with($with)->cache($cache); - if ($data instanceof \Closure) { + if (is_array($data) && key($data) !== 0) { + $result = $result->where($data); + $data = []; + } elseif ($data instanceof \Closure) { call_user_func_array($data, [ & $result]); $data = []; } elseif ($data instanceof Query) { @@ -832,19 +835,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param string $model 模型名 * @param string $table 中间表名 + * @param string $foreignKey 关联外键 * @param string $localKey 当前模型关联键 - * @param string $foreignKey 关联模型关联键 * @return \think\db\Query|string */ - public function belongsToMany($model, $table = '', $localKey = '', $foreignKey = '') + public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); $name = Loader::parseName(basename(str_replace('\\', '/', $model))); $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); - $localKey = $localKey ?: $name . '_id'; - $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey); + $foreignKey = $foreignKey ?: $name . '_id'; + $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; + return $this->relation->belongsToMany($model, $table, $foreignKey, $localKey); } /** diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 5065500b..22223dd2 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -88,7 +88,7 @@ class Relation // 关联查询 $pk = $this->parent->getPk(); $condition['pivot.' . $foreignKey] = $this->parent->$pk; - $result = $this->belongsToManyQuery($relation, $this->middle, $localKey, $foreignKey, $condition)->select(); + $result = $this->belongsToManyQuery($relation, $this->middle, $foreignKey, $localKey, $condition)->select(); foreach ($result as $set) { $pivot = []; foreach ($set->toArray() as $key => $val) { @@ -333,7 +333,7 @@ class Relation $foreignKey = $this->foreignKey; $localKey = $this->localKey; // 预载入关联查询 支持嵌套预载入 - $list = $this->belongsToManyQuery($model, $this->middle, $localKey, $foreignKey, $where)->with($subRelation)->select(); + $list = $this->belongsToManyQuery($model, $this->middle, $foreignKey, $localKey, $where)->with($subRelation)->select(); // 组装模型数据 $data = []; @@ -360,7 +360,7 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @return \think\db\Query|string + * @return $this */ public function hasOne($model, $foreignKey, $localKey) { @@ -379,7 +379,7 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $otherKey 关联主键 - * @return \think\db\Query|string + * @return $this */ public function belongsTo($model, $foreignKey, $otherKey) { @@ -399,7 +399,7 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 - * @return \think\db\Query|string + * @return $this */ public function hasMany($model, $foreignKey, $localKey) { @@ -418,11 +418,11 @@ class Relation * @access public * @param string $model 模型名 * @param string $table 中间表名 + * @param string $foreignKey 关联模型外键 * @param string $localKey 当前模型关联键 - * @param string $foreignKey 关联模型关联键 - * @return \think\db\Query|string + * @return $this */ - public function belongsToMany($model, $table, $localKey, $foreignKey) + public function belongsToMany($model, $table, $foreignKey, $localKey) { // 记录当前关联信息 $this->type = self::BELONGS_TO_MANY; @@ -440,19 +440,19 @@ class Relation * @access public * @param object $model 关联模型对象 * @param string $table 中间表名 - * @param string $localKey 当前模型关联键 * @param string $foreignKey 关联模型关联键 + * @param string $localKey 当前模型关联键 * @param array $condition 关联查询条件 * @return \think\db\Query|string */ - protected function belongsToManyQuery($model, $table, $localKey, $foreignKey, $condition = []) + protected function belongsToManyQuery($model, $table, $foreignKey, $localKey, $condition = []) { // 关联查询封装 $tableName = $model->getTable(); $relationFk = $model->getPk(); return $model::field($tableName . '.*') ->field(true, false, $table, 'pivot', 'pivot__') - ->join($table . ' pivot', 'pivot.' . $localKey . '=' . $tableName . '.' . $relationFk) + ->join($table . ' pivot', 'pivot.' . $foreignKey . '=' . $tableName . '.' . $relationFk) ->where($condition); } diff --git a/tests/thinkphp/library/think/template/taglib/cxTest.php b/tests/thinkphp/library/think/template/taglib/cxTest.php index e40d3de4..34f296a6 100644 --- a/tests/thinkphp/library/think/template/taglib/cxTest.php +++ b/tests/thinkphp/library/think/template/taglib/cxTest.php @@ -47,7 +47,7 @@ EOF; {/volist} EOF; $data = <<\$vo): \$mod = (\$key % 2 );++\$key;?> +\$vo): \$mod = (\$key % 2 );++\$key;?> EOF; @@ -88,9 +88,9 @@ EOF; {/foreach} EOF; $data = <<\$val): ?> +\$val): ?> - + EOF; $cx->parseTag($content); $this->assertEquals($content, $data); @@ -389,7 +389,7 @@ default {/empty} EOF; $data = << +isEmpty())): ?> default EOF; @@ -402,7 +402,7 @@ default {/notempty} EOF; $data = << +isEmpty()))): ?> default EOF;