Skip to content

Commit f3f7fef

Browse files
authored
Merge pull request #15 from roxblnfk/feature/order-by-in-relations
Add supporting of orderBy option in relations
2 parents 8e09b3c + a2695e7 commit f3f7fef

7 files changed

Lines changed: 43 additions & 20 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"require": {
77
"php": ">=7.2",
88
"spiral/tokenizer": "^2.7",
9-
"cycle/schema-builder": "^1.0",
9+
"cycle/schema-builder": "^1.2",
1010
"doctrine/annotations": "^1.7"
1111
},
1212
"require-dev": {

src/Annotation/Relation/HasMany.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @Attribute("innerKey", type="string"),
2626
* @Attribute("outerKey", type="string"),
2727
* @Attribute("where", type="array"),
28+
* @Attribute("orderBy", type="array"),
2829
* @Attribute("fkCreate", type="bool"),
2930
* @Attribute("fkAction", type="string"),
3031
* @Attribute("indexCreate", type="bool"),
@@ -53,6 +54,9 @@ final class HasMany extends Relation
5354
/** @var array */
5455
protected $where;
5556

57+
/** @var array */
58+
protected $orderBy;
59+
5660
/** @var bool */
5761
protected $fkCreate;
5862

src/Annotation/Relation/ManyToMany.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @Attribute("outerKey", type="string"),
2828
* @Attribute("innerKey", type="string"),
2929
* @Attribute("where", type="array"),
30+
* @Attribute("orderBy", type="array"),
3031
* @Attribute("though", type="string", required=true),
3132
* @Attribute("thoughInnerKey", type="string"),
3233
* @Attribute("thoughOuterKey", type="string"),
@@ -58,6 +59,9 @@ final class ManyToMany extends Relation
5859
/** @var array */
5960
protected $where;
6061

62+
/** @var array */
63+
protected $orderBy;
64+
6165
/** @var string */
6266
protected $though;
6367

tests/Annotated/Fixtures/Simple.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Simple implements LabelledInterface
3737
protected $one;
3838

3939
/**
40-
* @HasMany(target="WithTable", where={"id": {">": 1}})
40+
* @HasMany(target="WithTable", where={"id": {">=": 1}}, orderBy={"id": "DESC"})
4141
* @var WithTable[]|Collection
4242
*/
4343
protected $many;

tests/Annotated/Fixtures/WithTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class WithTable implements LabelledInterface
3636
/** @Column(type="primary") */
3737
protected $id;
3838

39-
/** @ManyToMany(target="Tag", though="Tag/Context") */
39+
/** @ManyToMany(target="Tag", though="Tag/Context", where={"id": {">=": "1"}}, orderBy={"id": "DESC"}) */
4040
protected $tags;
4141

4242
/** @MorphedHasMany(target="Label", outerKey="owner_id", morphKey="owner_role", indexCreate=false) */

tests/Annotated/Relation/HasManyTest.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,31 @@ public function testRelation(): void
3232
{
3333
$r = new Registry($this->dbal);
3434

35-
$schema = (new Compiler())->compile($r, [
36-
new Entities($this->locator),
37-
new ResetTables(),
38-
new MergeColumns(),
39-
new GenerateRelations(),
40-
new RenderTables(),
41-
new RenderRelations(),
42-
new MergeIndexes(),
43-
new SyncTables(),
44-
new GenerateTypecast(),
45-
]);
35+
$schema = (new Compiler())->compile(
36+
$r,
37+
[
38+
new Entities($this->locator),
39+
new ResetTables(),
40+
new MergeColumns(),
41+
new GenerateRelations(),
42+
new RenderTables(),
43+
new RenderRelations(),
44+
new MergeIndexes(),
45+
new SyncTables(),
46+
new GenerateTypecast(),
47+
]
48+
);
4649

4750
$this->assertArrayHasKey('many', $schema['simple'][Schema::RELATIONS]);
4851
$this->assertSame(Relation::HAS_MANY, $schema['simple'][Schema::RELATIONS]['many'][Relation::TYPE]);
4952
$this->assertSame('withTable', $schema['simple'][Schema::RELATIONS]['many'][Relation::TARGET]);
50-
51-
$this->assertSame([
52-
'id' => ['>' => 1]
53-
], $schema['simple'][Schema::RELATIONS]['many'][Relation::SCHEMA][Relation::WHERE]);
53+
$this->assertSame(
54+
["id" => [">=" => 1]],
55+
$schema['simple'][Schema::RELATIONS]['many'][Relation::SCHEMA][Relation::WHERE]
56+
);
57+
$this->assertSame(
58+
["id" => "DESC"],
59+
$schema['simple'][Schema::RELATIONS]['many'][Relation::SCHEMA][Relation::ORDER_BY]
60+
);
5461
}
5562
}

tests/Annotated/Relation/ManyToManyTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,18 @@ public function testRelation(): void
5757
$schema['withTable'][Schema::RELATIONS]['tags'][Relation::TARGET]
5858
);
5959

60-
6160
$this->assertSame(
6261
'tagContext',
63-
$schema['withTable'][Schema::RELATIONS]['tags'][Relation::SCHEMA][Relation::THOUGH_ENTITY]
62+
$schema['withTable'][Schema::RELATIONS]['tags'][Relation::SCHEMA][Relation::THROUGH_ENTITY]
63+
);
64+
65+
$this->assertSame(
66+
["id" => [">=" => "1"]],
67+
$schema['withTable'][Schema::RELATIONS]['tags'][Relation::SCHEMA][Relation::WHERE]
68+
);
69+
$this->assertSame(
70+
["id" => "DESC"],
71+
$schema['withTable'][Schema::RELATIONS]['tags'][Relation::SCHEMA][Relation::ORDER_BY]
6472
);
6573
}
6674
}

0 commit comments

Comments
 (0)