改进多对多关联查询及预载入

This commit is contained in:
thinkphp
2016-04-17 22:34:12 +08:00
parent 14e44291e4
commit 3f7113558c
3 changed files with 104 additions and 20 deletions

View File

@@ -803,17 +803,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* BELONGS TO MANY 关联定义
* @access public
* @param string $model 模型名
* @param string $localKey 关联主键
* @param string $foreignKey 关联
* @param string $table 中间表名
* @param string $localKey 当前模型关联键
* @param string $foreignKey 关联模型关联键
* @return \think\db\Query|string
*/
public function belongsToMany($model, $localKey = '', $foreignKey = '')
public function belongsToMany($model, $table = '', $localKey = '', $foreignKey = '')
{
// 记录当前关联信息
$model = $this->parseModel($model);
$foreignKey = $foreignKey ?: $this->pk;
$table = $table ?: Db::name(strtolower($this->name . '_' . basename(str_replace('\\', '/', $model))))->getTableName();
$localKey = $localKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id';
return $this->relation->belongsToMany($model, $foreignKey, $localKey);
$foreignKey = $foreignKey ?: strtolower($this->name) . '_id';
return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey);
}
/**