Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 3 additions & 6 deletions src/Select/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Cycle\ORM\Schema;
use Cycle\ORM\Select\Traits\AliasTrait;
use Cycle\ORM\Select\Traits\ChainTrait;
use Cycle\ORM\Select\Traits\ConstrainTrait;
use Spiral\Database\Query\SelectQuery;

/**
Expand All @@ -45,6 +46,7 @@ abstract class AbstractLoader implements LoaderInterface
{
use ChainTrait;
use AliasTrait;
use ConstrainTrait;

// Loading methods for data loaders.
public const INLOAD = 1;
Expand Down Expand Up @@ -81,6 +83,7 @@ public function __construct(ORMInterface $orm, string $target)
{
$this->orm = $orm;
$this->target = $target;
$this->setConstrain($this->getSource()->getConstrain());
}

/**
Expand Down Expand Up @@ -294,12 +297,6 @@ protected function configureQuery(SelectQuery $query): SelectQuery
return $query;
}

/**
* @param SelectQuery $query
* @return SelectQuery
*/
abstract protected function applyConstrain(SelectQuery $query): SelectQuery;

/**
* Define schema option associated with the entity.
*
Expand Down
4 changes: 0 additions & 4 deletions src/Select/JoinableLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Cycle\ORM\Parser\AbstractNode;
use Cycle\ORM\Schema;
use Cycle\ORM\Select\Traits\ColumnsTrait;
use Cycle\ORM\Select\Traits\ConstrainTrait;
use Spiral\Database\Query\SelectQuery;
use Spiral\Database\StatementInterface;

Expand All @@ -26,7 +25,6 @@
abstract class JoinableLoader extends AbstractLoader implements JoinableInterface
{
use ColumnsTrait;
use ConstrainTrait;

/**
* Default set of relation options. Child implementation might defined their of default options.
Expand Down Expand Up @@ -128,8 +126,6 @@ public function withContext(LoaderInterface $parent, array $options = []): Loade
} elseif (is_string($loader->options['constrain'])) {
$loader->setConstrain($this->orm->getFactory()->make($loader->options['constrain']));
}
} else {
$loader->setConstrain($this->getSource()->getConstrain());
}

if ($loader->isLoaded()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Select/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function resolve(string $identifier, bool $autoload = true): string
$loader = $this->findLoader(substr($identifier, 0, $split), $autoload);
if ($loader !== null) {
return sprintf(
'%s.%s.',
'%s.%s',
$loader->getAlias(),
$loader->fieldAlias(substr($identifier, $split + 1))
);
Expand Down
2 changes: 0 additions & 2 deletions src/Select/RootLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Cycle\ORM\Parser\Typecast;
use Cycle\ORM\Schema;
use Cycle\ORM\Select\Traits\ColumnsTrait;
use Cycle\ORM\Select\Traits\ConstrainTrait;
use Spiral\Database\Query\SelectQuery;
use Spiral\Database\StatementInterface;

Expand All @@ -30,7 +29,6 @@
final class RootLoader extends AbstractLoader
{
use ColumnsTrait;
use ConstrainTrait;

/** @var array */
protected $options = [
Expand Down
23 changes: 23 additions & 0 deletions tests/ORM/Fixtures/SortByIdDescConstrain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Fixtures;

use Cycle\ORM\Select\ConstrainInterface;
use Cycle\ORM\Select\QueryBuilder;

class SortByIdDescConstrain implements ConstrainInterface
{
public function apply(QueryBuilder $query): void
{
$query->orderBy('id', 'DESC');
}
}
113 changes: 80 additions & 33 deletions tests/ORM/HasManyRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Cycle\ORM\Select\JoinableLoader;
use Cycle\ORM\Tests\Fixtures\Comment;
use Cycle\ORM\Tests\Fixtures\SortByIDConstrain;
use Cycle\ORM\Tests\Fixtures\SortByIdDescConstrain;
use Cycle\ORM\Tests\Fixtures\User;
use Cycle\ORM\Tests\Traits\TableTrait;
use Cycle\ORM\Transaction;
Expand Down Expand Up @@ -65,39 +66,7 @@ public function setUp(): void
]
);

$this->orm = $this->withSchema(new Schema([
User::class => [
Schema::ROLE => 'user',
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'email', 'balance'],
Schema::SCHEMA => [],
Schema::RELATIONS => [
'comments' => [
Relation::TYPE => Relation::HAS_MANY,
Relation::TARGET => Comment::class,
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::INNER_KEY => 'id',
Relation::OUTER_KEY => 'user_id',
],
]
]
],
Comment::class => [
Schema::ROLE => 'comment',
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'comment',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'user_id', 'message'],
Schema::SCHEMA => [],
Schema::RELATIONS => [],
Schema::CONSTRAIN => SortByIDConstrain::class
]
]));
$this->orm = $this->withSchema(new Schema($this->getSchemaArray()));
}

public function testInitRelation(): void
Expand Down Expand Up @@ -143,6 +112,47 @@ public function testFetchRelation(): void
], $selector->fetchData());
}

public function testFetchRelationWithScopeAppliedByDefault(): void
{
$schemaArray = $this->getSchemaArray();
$schemaArray[User::class][Schema::CONSTRAIN] = SortByIdDescConstrain::class;
$schemaArray[Comment::class][Schema::CONSTRAIN] = SortByIdDescConstrain::class;
$this->withSchema(new Schema($schemaArray));

$selector = new Select($this->orm, User::class);
$selector->load('comments');

$this->assertEquals([
[
'id' => 2,
'email' => '[email protected]',
'balance' => 200.0,
'comments' => [],
],
[
'id' => 1,
'email' => '[email protected]',
'balance' => 100.0,
'comments' => [
[
'id' => 3,
'user_id' => 1,
'message' => 'msg 3',
],
[
'id' => 2,
'user_id' => 1,
'message' => 'msg 2',
],
[
'id' => 1,
'user_id' => 1,
'message' => 'msg 1',
],
],
],
], $selector->fetchData());
}

public function testFetchRelationInload(): void
{
Expand Down Expand Up @@ -472,4 +482,41 @@ public function testSliceAndSaveToAnotherParent(): void

$this->assertEquals('new b', $b->comments[0]->message);
}

private function getSchemaArray(): array
{
return [
User::class => [
Schema::ROLE => 'user',
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'email', 'balance'],
Schema::SCHEMA => [],
Schema::RELATIONS => [
'comments' => [
Relation::TYPE => Relation::HAS_MANY,
Relation::TARGET => Comment::class,
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::INNER_KEY => 'id',
Relation::OUTER_KEY => 'user_id',
],
]
]
],
Comment::class => [
Schema::ROLE => 'comment',
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'comment',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'user_id', 'message'],
Schema::SCHEMA => [],
Schema::RELATIONS => [],
Schema::CONSTRAIN => SortByIDConstrain::class
]
];
}
}