mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-09-02 21:41:36 +08:00
改进多对多关联查询及预载入
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user