/** * Add select with alias to query from "has one" relation * * @param Builder $query * @param HasOne $relatedModel */ private function addHasOneSelect(Builder $query, HasOne $relatedModel) { $query->getQuery()->join($relatedModel->getRelated()->getTable(), $relatedModel->getRelated()->getTable() . '.' . $relatedModel->getRelated()->getKeyName(), '=', $relatedModel->getQualifiedParentKeyName()); }
protected function applyHasOne(Query $query, Model $model, HasOne $hasOne, $name) { if (isset($this->joinClasses[$name])) { return; } $modelTable = $model->getTable(); $related = $hasOne->getRelated(); $relatedTable = $related->getTable(); $foreignKey = $hasOne->getPlainForeignKey(); $qualifiedLocalKey = $hasOne->getQualifiedParentKeyName(); list($parentTable, $localKey) = explode('.', $qualifiedLocalKey); if ($this->parser->isRelatedKey($name)) { list($parentPath, $key) = $this->parser->toJoinAndKey($name); $this->addJoinOnce($query, $this->model, $parentPath); $qualifiedLocalKey = $this->joinAliases[$parentPath] . ".{$localKey}"; } else { $qualifiedLocalKey = $hasOne->getQualifiedParentKeyName(); } $alias = $this->joinNameToAlias($name); $joinMethod = $this->getJoinMethod($name); $query->{$joinMethod}("{$relatedTable} AS {$alias}", "{$qualifiedLocalKey}", '=', "{$alias}.{$foreignKey}"); $query->distinct(); $this->joinClasses[$name] = $hasOne->getRelated(); $this->joinTable[$name] = $relatedTable; $this->joinAliases[$name] = $alias; }