Skip to content

Commit ea1fa2f

Browse files
committed
fix case where PK != 'id'
1 parent 7e6871d commit ea1fa2f

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

LeanMapperQuery/Query.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,17 @@ protected function parseStatement($statement, $replacePlaceholders = NULL)
334334
// NOTE: Traversing to a related entity is necessary even for the HasOne and HasMany
335335
// relationships if there are implicit filters to be applied.
336336
$this->triggerJoin();
337-
list(, $properties) = $this->traverseToRelatedEntity($tableName, $tableNameAlias, $property);
337+
list($entityClass, $properties) = $this->traverseToRelatedEntity($tableName, $tableNameAlias, $property);
338338
$column = $this->mapper->getPrimaryKey($tableName);
339-
$property = $properties[$column];
339+
$property = NULL;
340+
foreach ($properties as $prop) {
341+
if ($prop->getColumn() === $column) {
342+
$property = $prop;
343+
}
344+
}
345+
if (!$property) {
346+
throw new InvalidStateException("Entity '$entityClass' doesn't have any field corresponding to the primary key column '$column'.");
347+
}
340348
} else {
341349
$column = $property->getColumn();
342350
if ($column === NULL) {

tests/LeanMapperQuery/Query.joins.phpt

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ use Tester\Assert;
1010

1111
require_once __DIR__ . '/../bootstrap.php';
1212

13+
class Test2Mapper extends TestMapper
14+
{
15+
public function getPrimaryKey($table)
16+
{
17+
if ($table === 'author') {
18+
return 'id_author';
19+
}
20+
return 'id';
21+
}
22+
23+
public function getRelationshipColumn($sourceTable, $targetTable)
24+
{
25+
return $targetTable . '_id';
26+
}
27+
}
28+
$mapper = new Test2Mapper;
29+
1330
/**
1431
* @property int $id
1532
* @property string $name
@@ -34,16 +51,24 @@ class Book extends Entity
3451
}
3552

3653
/**
37-
* @property int $id
54+
* @property int $id (id_author)
3855
* @property string $name
3956
* @property Book[] $books m:belongsToMany
4057
* @property Book[] $reviewedBooks m:belongsToMany(reviewer_id)
58+
* @property Foo $foo m:hasOne
4159
* @property string|NULL $web
4260
*/
4361
class Author extends Entity
4462
{
4563
}
4664

65+
/**
66+
* @property int $id (id_foo)
67+
*/
68+
class Foo extends Entity
69+
{
70+
}
71+
4772
//////// Basic joins ////////
4873

4974
// HasOne relationship
@@ -53,7 +78,7 @@ $query->where('@author.name', 'Karel')
5378
->applyQuery($fluent, $mapper);
5479

5580
$expected = getFluent('book')
56-
->leftJoin('author')->on('[book].[author_id] = [author].[id]')
81+
->leftJoin('author')->on('[book].[author_id] = [author].[id_author]')
5782
->where("([author].[name] = 'Karel')");
5883
Assert::same((string) $expected, (string) $fluent);
5984

@@ -64,7 +89,7 @@ getQuery()
6489
->applyQuery($fluent, $mapper);
6590

6691
$expected = getFluent('author')
67-
->leftJoin('book')->on('[author].[id] = [book].[author_id]')
92+
->leftJoin('book')->on('[author].[id_author] = [book].[author_id]')
6893
->where('([book].[available] = 1)');
6994
Assert::same((string) $expected, (string) $fluent);
7095

@@ -87,8 +112,8 @@ $query->where('@reviewer.web', 'http://leanmapper.com')
87112
->applyQuery($fluent, $mapper);
88113

89114
$expected = getFluent('book')
90-
->leftJoin('author')->on('[book].[author_id] = [author].[id]')
91-
->leftJoin('[author] [author_reviewer_id]')->on('[book].[reviewer_id] = [author_reviewer_id].[id]')
115+
->leftJoin('author')->on('[book].[author_id] = [author].[id_author]')
116+
->leftJoin('[author] [author_reviewer_id]')->on('[book].[reviewer_id] = [author_reviewer_id].[id_author]')
92117
->where("([author].[name] = 'Karel')")
93118
->where("([author_reviewer_id].[web] = 'http://leanmapper.com')");
94119
Assert::same((string) $expected, (string) $fluent);
@@ -123,7 +148,7 @@ getQuery()
123148
->applyQuery($fluent, $mapper);
124149

125150
$expected = getFluent('author')
126-
->leftJoin('book')->on('[author].[id] = [book].[author_id]')
151+
->leftJoin('book')->on('[author].[id_author] = [book].[author_id]')
127152
->where('([book].[id] = 2)');
128153
Assert::same((string) $expected, (string) $fluent);
129154

@@ -135,13 +160,19 @@ getQuery()
135160
->applyQuery($fluent, $mapper);
136161

137162
$expected = getFluent('book')
138-
->leftJoin('author')->on('[book].[author_id] = [author].[id]')
139-
->leftJoin('[book] [book_id]')->on('[author].[id] = [book_id].[author_id]')
140-
->leftJoin('book_tag')->on('[book_id].[id] = [book_tag].[book_id]')
163+
->leftJoin('author')->on('[book].[author_id] = [author].[id_author]')
164+
->leftJoin('[book] [book_id_author]')->on('[author].[id_author] = [book_id_author].[author_id]')
165+
->leftJoin('book_tag')->on('[book_id_author].[id] = [book_tag].[book_id]')
141166
->leftJoin('tag')->on('[book_tag].[tag_id] = [tag].[id]')
142167
->where("([tag].[name] = 'foo')");
143168
Assert::same((string) $expected, (string) $fluent);
144169

170+
Assert::throws(function () use ($mapper){
171+
getQuery()
172+
->where('@foo', 3)
173+
->applyQuery(getFluent('author'), $mapper);
174+
}, 'LeanMapperQuery\\Exception\\InvalidStateException', "Entity 'Foo' doesn't have any field corresponding to the primary key column 'id'.");
175+
145176
$fluent = getFluent('book');
146177
getQuery()
147178
->where('@author', 2)

0 commit comments

Comments
 (0)