From 09d1ab24224892bc5d7ab5ea0c344fcf49185bac Mon Sep 17 00:00:00 2001 From: Andriy Kutsynyak Date: Thu, 23 Mar 2023 11:21:23 -0500 Subject: [PATCH 1/2] Fixed belongs to many relation --- src/Driver/AbstractDriver.php | 8 ++++++-- src/Property.php | 2 ++ src/Relation/BelongsToMany.php | 6 ++++-- src/Relation/Relationship.php | 3 ++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php index 45bcc4e..84865a8 100644 --- a/src/Driver/AbstractDriver.php +++ b/src/Driver/AbstractDriver.php @@ -8,6 +8,7 @@ use Pulsar\Model; use Pulsar\Property; use Pulsar\Query; +use Pulsar\Relation\Pivot; use Pulsar\Type; use UnitEnum; @@ -122,9 +123,12 @@ protected function addJoins(Query $query, string $tablename, SelectQuery $dbQuer foreach ($query->getJoins() as $join) { [$foreignModelClass, $column, $foreignKey, $type] = $join; - $foreignModel = new $foreignModelClass(); + + $foreignModel = $foreignModelClass instanceof Pivot ? $foreignModelClass : new $foreignModelClass(); $foreignTablename = $foreignModel->getTablename(); - $condition = $this->prefixColumn($column, $tablename).'='.$this->prefixColumn($foreignKey, $foreignTablename); + $condition = $foreignModelClass instanceof Pivot + ? $this->prefixColumn($column, $foreignTablename).'='.$this->prefixColumn($foreignKey, $tablename) + : $this->prefixColumn($column, $tablename).'='.$this->prefixColumn($foreignKey, $foreignTablename); $dbQuery->join($foreignTablename, $condition, null, $type); } diff --git a/src/Property.php b/src/Property.php index 04a4ccf..5dab1b2 100644 --- a/src/Property.php +++ b/src/Property.php @@ -35,6 +35,7 @@ public function __construct( ?string $relation_type = null, public readonly ?string $foreign_key = null, public readonly ?string $local_key = null, + public readonly ?string $id_key = null, public readonly ?string $pivot_tablename = null, public readonly ?array $morphs_to = null, ?string $belongs_to = null, @@ -113,6 +114,7 @@ public function toArray(): array 'relation_type' => $this->relation_type, 'foreign_key' => $this->foreign_key, 'local_key' => $this->local_key, + 'id_key' => $this->id_key, 'pivot_tablename' => $this->pivot_tablename, 'morphs_to' => $this->morphs_to, 'enum_class' => $this->enum_class, diff --git a/src/Relation/BelongsToMany.php b/src/Relation/BelongsToMany.php index 50ff706..286bcf5 100644 --- a/src/Relation/BelongsToMany.php +++ b/src/Relation/BelongsToMany.php @@ -28,7 +28,7 @@ final class BelongsToMany extends AbstractRelation * @param string $foreignModel foreign model class * @param string $foreignKey identifying key on foreign model */ - public function __construct(Model $localModel, string $localKey, string $tablename, string $foreignModel, string $foreignKey) + public function __construct(Model $localModel, string $localKey, string $tablename, string $foreignModel, string $foreignKey, private readonly string|null $idKey) { $this->tablename = $tablename; @@ -41,12 +41,14 @@ protected function initQuery(Query $query): Query $pivot->setTablename($this->tablename); $ids = $this->localModel->ids(); + $ids = $this->idKey ? [$this->idKey => array_shift($ids)] : $ids ; + //known issue - this will work only on single join column foreach ($ids as $idProperty => $id) { if (null === $id) { $this->empty = true; } - $query->where($this->localKey, $id); + $query->where("$this->tablename.$this->localKey = $id"); $query->join($pivot, $this->foreignKey, $idProperty); } diff --git a/src/Relation/Relationship.php b/src/Relation/Relationship.php index 0f41f08..8fe6b20 100644 --- a/src/Relation/Relationship.php +++ b/src/Relation/Relationship.php @@ -34,8 +34,9 @@ public static function make(Model $model, Property $property): AbstractRelation if (self::BELONGS_TO_MANY == $type) { $pivotTable = $property->pivot_tablename; + $idKey = $property->id_key; - return new BelongsToMany($model, $localKey, $pivotTable, $foreignModel, $foreignKey); + return new BelongsToMany($model, $localKey, $pivotTable, $foreignModel, $foreignKey, $idKey); } if (self::HAS_ONE == $type) { From fa6aee9156848fc9def66f4a9c9cfbec8de1b84a Mon Sep 17 00:00:00 2001 From: Andriy Kutsynyak Date: Thu, 23 Mar 2023 11:34:20 -0500 Subject: [PATCH 2/2] Remove id key --- src/Property.php | 2 -- src/Relation/BelongsToMany.php | 6 ++---- src/Relation/Relationship.php | 3 +-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Property.php b/src/Property.php index 5dab1b2..04a4ccf 100644 --- a/src/Property.php +++ b/src/Property.php @@ -35,7 +35,6 @@ public function __construct( ?string $relation_type = null, public readonly ?string $foreign_key = null, public readonly ?string $local_key = null, - public readonly ?string $id_key = null, public readonly ?string $pivot_tablename = null, public readonly ?array $morphs_to = null, ?string $belongs_to = null, @@ -114,7 +113,6 @@ public function toArray(): array 'relation_type' => $this->relation_type, 'foreign_key' => $this->foreign_key, 'local_key' => $this->local_key, - 'id_key' => $this->id_key, 'pivot_tablename' => $this->pivot_tablename, 'morphs_to' => $this->morphs_to, 'enum_class' => $this->enum_class, diff --git a/src/Relation/BelongsToMany.php b/src/Relation/BelongsToMany.php index 286bcf5..a789e38 100644 --- a/src/Relation/BelongsToMany.php +++ b/src/Relation/BelongsToMany.php @@ -28,7 +28,7 @@ final class BelongsToMany extends AbstractRelation * @param string $foreignModel foreign model class * @param string $foreignKey identifying key on foreign model */ - public function __construct(Model $localModel, string $localKey, string $tablename, string $foreignModel, string $foreignKey, private readonly string|null $idKey) + public function __construct(Model $localModel, string $localKey, string $tablename, string $foreignModel, string $foreignKey) { $this->tablename = $tablename; @@ -40,10 +40,8 @@ protected function initQuery(Query $query): Query $pivot = new Pivot(); $pivot->setTablename($this->tablename); - $ids = $this->localModel->ids(); - $ids = $this->idKey ? [$this->idKey => array_shift($ids)] : $ids ; //known issue - this will work only on single join column - foreach ($ids as $idProperty => $id) { + foreach ($this->localModel->ids() as $idProperty => $id) { if (null === $id) { $this->empty = true; } diff --git a/src/Relation/Relationship.php b/src/Relation/Relationship.php index 8fe6b20..0f41f08 100644 --- a/src/Relation/Relationship.php +++ b/src/Relation/Relationship.php @@ -34,9 +34,8 @@ public static function make(Model $model, Property $property): AbstractRelation if (self::BELONGS_TO_MANY == $type) { $pivotTable = $property->pivot_tablename; - $idKey = $property->id_key; - return new BelongsToMany($model, $localKey, $pivotTable, $foreignModel, $foreignKey, $idKey); + return new BelongsToMany($model, $localKey, $pivotTable, $foreignModel, $foreignKey); } if (self::HAS_ONE == $type) {