This commit is contained in:
尘缘
2016-05-08 23:21:57 +08:00
4 changed files with 27 additions and 22 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
/composer.lock
/vendor
.idea .idea

View File

@@ -598,7 +598,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected static function parseQuery(&$data, $with, $cache) protected static function parseQuery(&$data, $with, $cache)
{ {
$result = self::with($with)->cache($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]); call_user_func_array($data, [ & $result]);
$data = []; $data = [];
} elseif ($data instanceof Query) { } elseif ($data instanceof Query) {
@@ -832,19 +835,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @access public * @access public
* @param string $model 模型名 * @param string $model 模型名
* @param string $table 中间表名 * @param string $table 中间表名
* @param string $foreignKey 关联外键
* @param string $localKey 当前模型关联键 * @param string $localKey 当前模型关联键
* @param string $foreignKey 关联模型关联键
* @return \think\db\Query|string * @return \think\db\Query|string
*/ */
public function belongsToMany($model, $table = '', $localKey = '', $foreignKey = '') public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '')
{ {
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$name = Loader::parseName(basename(str_replace('\\', '/', $model))); $name = Loader::parseName(basename(str_replace('\\', '/', $model)));
$table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable();
$localKey = $localKey ?: $name . '_id'; $foreignKey = $foreignKey ?: $name . '_id';
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $localKey = $localKey ?: Loader::parseName($this->name) . '_id';
return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey); return $this->relation->belongsToMany($model, $table, $foreignKey, $localKey);
} }
/** /**

View File

@@ -88,7 +88,7 @@ class Relation
// 关联查询 // 关联查询
$pk = $this->parent->getPk(); $pk = $this->parent->getPk();
$condition['pivot.' . $foreignKey] = $this->parent->$pk; $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) { foreach ($result as $set) {
$pivot = []; $pivot = [];
foreach ($set->toArray() as $key => $val) { foreach ($set->toArray() as $key => $val) {
@@ -333,7 +333,7 @@ class Relation
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
$localKey = $this->localKey; $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 = []; $data = [];
@@ -360,7 +360,7 @@ class Relation
* @param string $model 模型名 * @param string $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $localKey 关联主键 * @param string $localKey 关联主键
* @return \think\db\Query|string * @return $this
*/ */
public function hasOne($model, $foreignKey, $localKey) public function hasOne($model, $foreignKey, $localKey)
{ {
@@ -379,7 +379,7 @@ class Relation
* @param string $model 模型名 * @param string $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $otherKey 关联主键 * @param string $otherKey 关联主键
* @return \think\db\Query|string * @return $this
*/ */
public function belongsTo($model, $foreignKey, $otherKey) public function belongsTo($model, $foreignKey, $otherKey)
{ {
@@ -399,7 +399,7 @@ class Relation
* @param string $model 模型名 * @param string $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $localKey 关联主键 * @param string $localKey 关联主键
* @return \think\db\Query|string * @return $this
*/ */
public function hasMany($model, $foreignKey, $localKey) public function hasMany($model, $foreignKey, $localKey)
{ {
@@ -418,11 +418,11 @@ class Relation
* @access public * @access public
* @param string $model 模型名 * @param string $model 模型名
* @param string $table 中间表名 * @param string $table 中间表名
* @param string $foreignKey 关联模型外键
* @param string $localKey 当前模型关联键 * @param string $localKey 当前模型关联键
* @param string $foreignKey 关联模型关联键 * @return $this
* @return \think\db\Query|string
*/ */
public function belongsToMany($model, $table, $localKey, $foreignKey) public function belongsToMany($model, $table, $foreignKey, $localKey)
{ {
// 记录当前关联信息 // 记录当前关联信息
$this->type = self::BELONGS_TO_MANY; $this->type = self::BELONGS_TO_MANY;
@@ -440,19 +440,19 @@ class Relation
* @access public * @access public
* @param object $model 关联模型对象 * @param object $model 关联模型对象
* @param string $table 中间表名 * @param string $table 中间表名
* @param string $localKey 当前模型关联键
* @param string $foreignKey 关联模型关联键 * @param string $foreignKey 关联模型关联键
* @param string $localKey 当前模型关联键
* @param array $condition 关联查询条件 * @param array $condition 关联查询条件
* @return \think\db\Query|string * @return \think\db\Query|string
*/ */
protected function belongsToManyQuery($model, $table, $localKey, $foreignKey, $condition = []) protected function belongsToManyQuery($model, $table, $foreignKey, $localKey, $condition = [])
{ {
// 关联查询封装 // 关联查询封装
$tableName = $model->getTable(); $tableName = $model->getTable();
$relationFk = $model->getPk(); $relationFk = $model->getPk();
return $model::field($tableName . '.*') return $model::field($tableName . '.*')
->field(true, false, $table, 'pivot', 'pivot__') ->field(true, false, $table, 'pivot', 'pivot__')
->join($table . ' pivot', 'pivot.' . $localKey . '=' . $tableName . '.' . $relationFk) ->join($table . ' pivot', 'pivot.' . $foreignKey . '=' . $tableName . '.' . $relationFk)
->where($condition); ->where($condition);
} }

View File

@@ -47,7 +47,7 @@ EOF;
{/volist} {/volist}
EOF; EOF;
$data = <<<EOF $data = <<<EOF
<?php if(is_array(\$list)): \$key = 0; \$__LIST__ = \$list;if( count(\$__LIST__)==0 ) : echo "" ;else: foreach(\$__LIST__ as \$key=>\$vo): \$mod = (\$key % 2 );++\$key;?> <?php if(is_array(\$list) || \$list instanceof \\think\\Collection): \$key = 0; \$__LIST__ = \$list;if( count(\$__LIST__)==0 ) : echo "" ;else: foreach(\$__LIST__ as \$key=>\$vo): \$mod = (\$key % 2 );++\$key;?>
<?php endforeach; endif; else: echo "" ;endif; ?> <?php endforeach; endif; else: echo "" ;endif; ?>
EOF; EOF;
@@ -88,9 +88,9 @@ EOF;
{/foreach} {/foreach}
EOF; EOF;
$data = <<<EOF $data = <<<EOF
<?php if(is_array(\$list)): foreach(\$list as \$key=>\$val): ?> <?php if(is_array(\$list) || \$list instanceof \\think\\Collection): if( count(\$list)==0 ) : echo "empty" ;else: foreach(\$list as \$key=>\$val): ?>
<?php endforeach; endif; ?><?php if(empty(\$list)): echo '"empty'; endif; ?> <?php endforeach; endif; else: echo "empty" ;endif; ?>
EOF; EOF;
$cx->parseTag($content); $cx->parseTag($content);
$this->assertEquals($content, $data); $this->assertEquals($content, $data);
@@ -389,7 +389,7 @@ default
{/empty} {/empty}
EOF; EOF;
$data = <<<EOF $data = <<<EOF
<?php if(empty(\$var)): ?> <?php if(empty(\$var) || (\$var instanceof \\think\\Collection && \$var->isEmpty())): ?>
default default
<?php endif; ?> <?php endif; ?>
EOF; EOF;
@@ -402,7 +402,7 @@ default
{/notempty} {/notempty}
EOF; EOF;
$data = <<<EOF $data = <<<EOF
<?php if(!empty(\$var)): ?> <?php if(!(empty(\$var) || (\$var instanceof \\think\\Collection && \$var->isEmpty()))): ?>
default default
<?php endif; ?> <?php endif; ?>
EOF; EOF;