Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Select/Loader/HasOneLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace Cycle\ORM\Select\Loader;

use Cycle\Database\Query\SelectQuery;
use Cycle\ORM\FactoryInterface;
use Cycle\ORM\Parser\AbstractNode;
use Cycle\ORM\Parser\SingularNode;
use Cycle\ORM\Service\SourceProviderInterface;
use Cycle\ORM\Relation;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Select\JoinableLoader;
use Cycle\ORM\Select\Traits\JoinOneTableTrait;
use Cycle\ORM\Select\Traits\OrderByTrait;
use Cycle\ORM\Select\Traits\WhereTrait;
use Cycle\Database\Query\SelectQuery;

/**
* Dedicated to load HAS_ONE relations, by default loader will prefer to join data into query.
Expand All @@ -25,6 +28,7 @@
class HasOneLoader extends JoinableLoader
{
use JoinOneTableTrait;
use OrderByTrait;
use WhereTrait;

/**
Expand All @@ -38,8 +42,22 @@ class HasOneLoader extends JoinableLoader
'as' => null,
'using' => null,
'where' => null,
'orderBy' => null,
];

public function __construct(
SchemaInterface $ormSchema,
SourceProviderInterface $sourceProvider,
FactoryInterface $factory,
string $name,
string $target,
array $schema
) {
parent::__construct($ormSchema, $sourceProvider, $factory, $name, $target, $schema);
$this->options['where'] = $schema[Relation::WHERE] ?? [];
$this->options['orderBy'] = $schema[Relation::ORDER_BY] ?? [];
}

public function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery
{
if ($this->options['using'] !== null) {
Expand All @@ -56,6 +74,13 @@ public function configureQuery(SelectQuery $query, array $outerKeys = []): Selec
$this->options['where'] ?? $this->schema[Relation::WHERE] ?? []
);

// user specified ORDER_BY rules
$this->setOrderBy(
$query,
$this->getAlias(),
$this->options['orderBy'] ?? $this->schema[Relation::ORDER_BY] ?? []
);

return parent::configureQuery($query);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/ORM/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class User implements ImagedInterface
*/
public $lastComment;

/**
* @var Comment|null
*/
public $firstComment;

/**
* @var Collection|Comment[]
*/
Expand Down
Loading