Skip to content

Commit 88e58ae

Browse files
committed
恢复关联定义参数
1 parent e69ed01 commit 88e58ae

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/model/concern/RelationShip.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -544,32 +544,35 @@ public function relationCount(Query $query, array $relations, string $aggregate
544544
*
545545
* @param string $model 模型名
546546
* @param string $foreignKey 关联外键
547+
* @param string $localKey 当前主键
547548
*
548549
* @return HasOne
549550
*/
550-
public function hasOne(string $model, string $foreignKey = ''): HasOne
551+
public function hasOne(string $model, string $foreignKey = '', string $localKey = ''): HasOne
551552
{
552553
// 记录当前关联信息
553554
$model = $this->parseRelationModel($model);
555+
$localKey = $localKey ?: $this->getPk();
554556
$foreignKey = $foreignKey ?: $this->getForeignKey($this->getName());
555557

556-
return new HasOne($this, $model, $foreignKey, $this->getPk());
558+
return new HasOne($this, $model, $foreignKey, $localKey);
557559
}
558560

559561
/**
560562
* BELONGS TO 关联定义.
561563
*
562564
* @param string $model 模型名
563565
* @param string $foreignKey 关联外键
566+
* @param string $localKey 关联主键
564567
*
565568
* @return BelongsTo
566569
*/
567-
public function belongsTo(string $model, string $foreignKey = ''): BelongsTo
570+
public function belongsTo(string $model, string $foreignKey = '', string $localKey = ''): BelongsTo
568571
{
569572
// 记录当前关联信息
570573
$model = $this->parseRelationModel($model);
571574
$foreignKey = $foreignKey ?: $this->getForeignKey((new $model())->getName());
572-
$localKey = (new $model())->getPk();
575+
$localKey = $localKey ?: (new $model())->getPk();
573576
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
574577
$relation = Str::snake($trace[1]['function']);
575578

@@ -581,16 +584,18 @@ public function belongsTo(string $model, string $foreignKey = ''): BelongsTo
581584
*
582585
* @param string $model 模型名
583586
* @param string $foreignKey 关联外键
587+
* @param string $localKey 当前主键
584588
*
585589
* @return HasMany
586590
*/
587-
public function hasMany(string $model, string $foreignKey = ''): HasMany
591+
public function hasMany(string $model, string $foreignKey = '', string $localKey = ''): HasMany
588592
{
589593
// 记录当前关联信息
590594
$model = $this->parseRelationModel($model);
595+
$localKey = $localKey ?: $this->getPk();
591596
$foreignKey = $foreignKey ?: $this->getForeignKey($this->getName());
592597

593-
return new HasMany($this, $model, $foreignKey, $this->getPk());
598+
return new HasMany($this, $model, $foreignKey, $localKey);
594599
}
595600

596601
/**
@@ -600,6 +605,8 @@ public function hasMany(string $model, string $foreignKey = ''): HasMany
600605
* @param string $through 中间模型名
601606
* @param string $foreignKey 关联外键
602607
* @param string $throughKey 关联外键
608+
* @param string $localKey 当前主键
609+
* @param string $throughPk 中间表主键
603610
*
604611
* @return HasManyThrough
605612
*/
@@ -608,10 +615,10 @@ public function hasManyThrough(string $model, string $through, string $foreignKe
608615
// 记录当前关联信息
609616
$model = $this->parseRelationModel($model);
610617
$through = $this->parseRelationModel($through);
618+
$localKey = $localKey ?: $this->getPk();
611619
$foreignKey = $foreignKey ?: $this->getForeignKey($this->getName());
612620
$throughKey = $throughKey ?: $this->getForeignKey((new $through())->getName());
613-
$localKey = $this->getPk();
614-
$throughPk = (new $through())->getPk();
621+
$throughPk = $throughPk ?: (new $through())->getPk();
615622

616623
return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk);
617624
}
@@ -623,18 +630,20 @@ public function hasManyThrough(string $model, string $through, string $foreignKe
623630
* @param string $through 中间模型名
624631
* @param string $foreignKey 关联外键
625632
* @param string $throughKey 关联外键
633+
* @param string $localKey 当前主键
634+
* @param string $throughPk 中间表主键
626635
*
627636
* @return HasOneThrough
628637
*/
629-
public function hasOneThrough(string $model, string $through, string $foreignKey = '', string $throughKey = ''): HasOneThrough
638+
public function hasOneThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = ''): HasOneThrough
630639
{
631640
// 记录当前关联信息
632641
$model = $this->parseRelationModel($model);
633642
$through = $this->parseRelationModel($through);
643+
$localKey = $localKey ?: $this->getPk();
634644
$foreignKey = $foreignKey ?: $this->getForeignKey($this->getName());
635645
$throughKey = $throughKey ?: $this->getForeignKey((new $through())->getName());
636-
$localKey = $this->getPk();
637-
$throughPk = (new $through())->getPk();
646+
$throughPk = $throughPk ?: (new $through())->getPk();
638647

639648
return new HasOneThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk);
640649
}

0 commit comments

Comments
 (0)