Skip to content

Commit

Permalink
Merge 09d1ab2 into 66a96c1
Browse files Browse the repository at this point in the history
  • Loading branch information
andriy-invoiced committed Mar 23, 2023
2 parents 66a96c1 + 09d1ab2 commit 16d5c14
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Driver/AbstractDriver.php
Expand Up @@ -8,6 +8,7 @@
use Pulsar\Model;
use Pulsar\Property;
use Pulsar\Query;
use Pulsar\Relation\Pivot;
use Pulsar\Type;
use UnitEnum;

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Property.php
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/Relation/BelongsToMany.php
Expand Up @@ -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;

Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Relation/Relationship.php
Expand Up @@ -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) {
Expand Down

0 comments on commit 16d5c14

Please sign in to comment.