Skip to content

Commit f1067e9

Browse files
janpechamibk
authored andcommitted
Entity: Fix hasMany & ORDER BY, requires LM >=2.3 (BC break)
1 parent dd9e95d commit f1067e9

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

LeanMapperQuery/Entity.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,29 @@ protected function queryProperty($field, IQuery $query)
5959
$sourceReferencingColumn = $relationship->getColumnReferencingSourceTable();
6060
$targetReferencingColumn = $relationship->getColumnReferencingTargetTable();
6161
$targetTable = $relationship->getTargetTable();
62+
$targetPrimaryKey = $mapper->getPrimaryKey($targetTable);
6263
$rows = array();
64+
$resultRows = array();
65+
$targetResultProxy = NULL;
66+
6367
foreach ($this->row->referencing($relationshipTable, $sourceReferencingColumn) as $relationship) {
6468
$row = $relationship->referenced($targetTable, $targetReferencingColumn, new Filtering($filters));
65-
$row !== NULL && $rows[] = $row;
69+
if ($row !== NULL && $targetResultProxy === NULL) {
70+
$targetResultProxy = $row->getResultProxy();
71+
}
72+
$row !== NULL && $resultRows[$row->{$targetPrimaryKey}] = $row;
73+
}
74+
75+
if ($targetResultProxy) {
76+
foreach ($targetResultProxy as $rowId => $rowData) {
77+
if (isset($resultRows[$rowId])) {
78+
$rows[] = $resultRows[$rowId];
79+
}
80+
}
81+
} else {
82+
$rows = $resultRows;
6683
}
84+
6785
} else {
6886
throw new InvalidRelationshipException('Only BelongsToMany and HasMany relationships are supported when querying entity property. ' . get_class($relationship) . ' given.');
6987
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"require": {
1717
"php": ">=5.3.0",
18-
"tharos/leanmapper": ">=2.1"
18+
"tharos/leanmapper": ">=2.3"
1919
},
2020
"require-dev": {
2121
"nette/tester": "~1.2"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* Test: LeanMapperQuery\Query ordering methods.
5+
* @author Michal Bohuslávek
6+
*/
7+
8+
use LeanMapper\Repository;
9+
use LeanMapperQuery\Entity;
10+
use LeanMapperQuery\Query;
11+
use Tester\Assert;
12+
13+
require_once __DIR__ . '/../bootstrap.php';
14+
15+
/**
16+
* @property int $id
17+
* @property Tag[] $tags m:hasMany
18+
*/
19+
class Book extends Entity
20+
{
21+
public function queryTags(Query $query)
22+
{
23+
return $this->queryProperty('tags', $query);
24+
}
25+
}
26+
27+
/**
28+
* @property int $id
29+
* @property string $name
30+
*/
31+
class Tag extends Entity
32+
{
33+
}
34+
35+
class BookRepository extends Repository
36+
{
37+
public function get($id)
38+
{
39+
$row = $this->connection->select('*')
40+
->from($this->getTable())
41+
->where('%n = ?', $this->mapper->getPrimaryKey($this->getTable()), $id)
42+
->fetch();
43+
if ($row === FALSE) {
44+
return $row;
45+
}
46+
return $this->createEntity($row);
47+
}
48+
}
49+
50+
$bookRepository = new BookRepository($connection, $mapper, $entityFactory);
51+
$book = $bookRepository->get(1);
52+
53+
$query = getQuery()
54+
->orderBy('@name');
55+
56+
$tags = $book->queryTags($query);
57+
$names = array();
58+
59+
foreach ($tags as $tag) {
60+
$names[] = $tag->name;
61+
}
62+
63+
Assert::same(array('ebook', 'popular'), $names);

0 commit comments

Comments
 (0)