Skip to content

Commit

Permalink
Merge fa6aee9 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 + fa6aee9 commit 94543f8
Show file tree
Hide file tree
Showing 2 changed files with 9 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
6 changes: 3 additions & 3 deletions src/Relation/BelongsToMany.php
Expand Up @@ -40,13 +40,13 @@ protected function initQuery(Query $query): Query
$pivot = new Pivot();
$pivot->setTablename($this->tablename);

$ids = $this->localModel->ids();
foreach ($ids as $idProperty => $id) {
//known issue - this will work only on single join column
foreach ($this->localModel->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

0 comments on commit 94543f8

Please sign in to comment.