@@ -10,6 +10,23 @@ use Tester\Assert;
1010
1111require_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 */
4361class 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') " );
5883Assert::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) ' );
6994Assert::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') " );
94119Assert::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) ' );
128153Assert::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') " );
143168Assert::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 ' );
146177getQuery ()
147178 ->where ('@author ' , 2 )
0 commit comments