/** * @param DataMapper $data * @param callable|null $callback * @return mixed */ protected function getResult(DataMapper $data, callable $callback = null) { $manager = $data->getEntityManager(); $owner = $data->getEntityMapper(); $related = $manager->resolveEntityMapper($this->entityClass); if ($this->foreignKey === null) { $this->foreignKey = $owner->getForeignKey(); } $statement = new SQLStatement(); $select = new EntityQuery($manager, $related, $statement); $select->where($this->foreignKey)->is($data->getColumn($owner->getPrimaryKey())); if ($this->queryCallback !== null || $callback !== null) { $query = new Query($statement); if ($this->queryCallback !== null) { ($this->queryCallback)($query); } if ($callback !== null) { $callback($query); } } return $this->hasMany ? $select->all() : $select->get(); }
/** * @param DataMapper $data * @param callable|null $callback * @return mixed */ protected function getResult(DataMapper $data, callable $callback = null) { $manager = $data->getEntityManager(); $owner = $data->getEntityMapper(); $related = $manager->resolveEntityMapper($this->entityClass); if ($this->junctionTable === null) { $table = [$owner->getTable(), $related->getTable()]; sort($table); $this->junctionTable = implode('_', $table); } if ($this->juctionKey === null) { $this->juctionKey = $related->getForeignKey(); } if ($this->foreignKey === null) { $this->foreignKey = $owner->getForeignKey(); } if ($this->joinTable === null) { $this->joinTable = $related->getTable(); } if ($this->joinColumn === null) { $this->joinColumn = $related->getPrimaryKey(); } $statement = new SQLStatement(); $select = new class($manager, $related, $statement, $this->junctionTable) extends EntityQuery { protected $junctionTable; public function __construct(EntityManager $entityManager, EntityMapper $entityMapper, $statement, $table) { parent::__construct($entityManager, $entityMapper, $statement); $this->junctionTable = $table; } protected function buildQuery() { $this->locked = true; $this->sql->addTables([$this->junctionTable]); return $this; } protected function isReadOnly() : bool { return count($this->sql->getJoins()) > 1; } }; $select->join($this->joinTable, function (Join $join) { $join->on($this->junctionTable . '.' . $this->juctionKey, $this->joinTable . '.' . $this->joinColumn); })->where($this->junctionTable . '.' . $this->foreignKey)->is($data->getColumn($owner->getPrimaryKey())); $statement->addColumn($this->joinTable . '.*'); if ($this->queryCallback !== null || $callback !== null) { $query = new Query($statement); if ($this->queryCallback !== null) { ($this->queryCallback)($query); } if ($callback !== null) { $callback($query); } } return $this->hasMany ? $select->all() : $select->get(); }