Skip to content

Commit 64b8543

Browse files
committed
Adding id accessor
1 parent 287d2c7 commit 64b8543

File tree

5 files changed

+108
-69
lines changed

5 files changed

+108
-69
lines changed

phpunit.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<testsuites>
1414
<testsuite name="all">
1515
<directory>tests/</directory>
16+
<exclude>tests/MysqlRelationsTest.php</exclude>
1617
</testsuite>
1718
<testsuite name="schema">
1819
<directory>tests/SchemaTest.php</directory>
@@ -33,6 +34,7 @@
3334
</testsuite>
3435
<testsuite name="relations">
3536
<directory>tests/RelationsTest.php</directory>
37+
<directory>tests/MysqlRelationsTest.php</directory>
3638
</testsuite>
3739
</testsuites>
3840
</phpunit>

src/Jenssegers/Mongodb/Model.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
3737
*/
3838
protected static $resolver;
3939

40+
/**
41+
* Custom accessor for the model's id.
42+
*
43+
* @return string
44+
*/
45+
public function getIdAttribute($value)
46+
{
47+
// If there is an actual id attribute, then return that.
48+
if ($value) return $value;
49+
50+
return $this->getKey();
51+
}
52+
4053
/**
4154
* Convert a DateTime to a storable MongoDate object.
4255
*
@@ -132,7 +145,10 @@ public function setRawAttributes(array $attributes, $sync = false)
132145
{
133146
foreach($attributes as $key => &$value)
134147
{
135-
// Convert MongoId to string
148+
/**
149+
* MongoIds are converted to string to make it easier to pass
150+
* the id to other instances or relations.
151+
*/
136152
if ($value instanceof MongoId)
137153
{
138154
$value = (string) $value;
@@ -153,7 +169,11 @@ public function attributesToArray()
153169

154170
foreach ($attributes as &$value)
155171
{
156-
// Convert MongoDate to string
172+
/**
173+
* Here we convert MongoDate instances to string. This mimics
174+
* original SQL behaviour so that dates are formatted nicely
175+
* when your models are converted to JSON.
176+
*/
157177
if ($value instanceof MongoDate)
158178
{
159179
$value = $this->asDateTime($value)->format('Y-m-d H:i:s');

tests/ModelTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,13 @@ public function testDates()
334334
$this->assertEquals('John Doe', $user->name);
335335
}
336336

337+
public function testIdAttribute()
338+
{
339+
$user = User::create(array('name' => 'John Doe'));
340+
$this->assertEquals($user->id, $user->_id);
341+
342+
$user = User::create(array('id' => 'custom_id', 'name' => 'John Doe'));
343+
$this->assertNotEquals($user->id, $user->_id);
344+
}
345+
337346
}

tests/MysqlRelationsTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
class MysqlRelationsTest extends PHPUnit_Framework_TestCase {
4+
5+
public function setUp()
6+
{
7+
MysqlUser::executeSchema();
8+
MysqlBook::executeSchema();
9+
MysqlRole::executeSchema();
10+
}
11+
12+
public function tearDown()
13+
{
14+
MysqlUser::truncate();
15+
MysqlBook::truncate();
16+
MysqlRole::truncate();
17+
}
18+
19+
public function testMysqlRelations()
20+
{
21+
$user = new MysqlUser;
22+
$this->assertInstanceOf('MysqlUser', $user);
23+
$this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
24+
25+
// Mysql User
26+
$user->name = "John Doe";
27+
$user->save();
28+
$this->assertTrue(is_int($user->id));
29+
30+
// SQL has many
31+
$book = new Book(array('title' => 'Game of Thrones'));
32+
$user->books()->save($book);
33+
$user = MysqlUser::find($user->id); // refetch
34+
$this->assertEquals(1, count($user->books));
35+
36+
// MongoDB belongs to
37+
$book = $user->books()->first(); // refetch
38+
$this->assertEquals('John Doe', $book->mysqlAuthor->name);
39+
40+
// SQL has one
41+
$role = new Role(array('type' => 'admin'));
42+
$user->role()->save($role);
43+
$user = MysqlUser::find($user->id); // refetch
44+
$this->assertEquals('admin', $user->role->type);
45+
46+
// MongoDB beelongs to
47+
$role = $user->role()->first(); // refetch
48+
$this->assertEquals('John Doe', $role->mysqlUser->name);
49+
50+
// MongoDB User
51+
$user = new User;
52+
$user->name = "John Doe";
53+
$user->save();
54+
55+
// MongoDB has many
56+
$book = new MysqlBook(array('title' => 'Game of Thrones'));
57+
$user->mysqlBooks()->save($book);
58+
$user = User::find($user->_id); // refetch
59+
$this->assertEquals(1, count($user->mysqlBooks));
60+
61+
// SQL belongs to
62+
$book = $user->mysqlBooks()->first(); // refetch
63+
$this->assertEquals('John Doe', $book->author->name);
64+
65+
// MongoDB has one
66+
$role = new MysqlRole(array('type' => 'admin'));
67+
$user->mysqlRole()->save($role);
68+
$user = User::find($user->_id); // refetch
69+
$this->assertEquals('admin', $user->mysqlRole->type);
70+
71+
// SQL belongs to
72+
$role = $user->mysqlRole()->first(); // refetch
73+
$this->assertEquals('John Doe', $role->user->name);
74+
}
75+
}

tests/RelationsTest.php

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -259,71 +259,4 @@ public function testBelongsToManyCustom()
259259
$this->assertEquals($group->_id, $user->groups()->first()->_id);
260260
$this->assertEquals($user->_id, $group->users()->first()->_id);
261261
}
262-
263-
public function testMysqlModel()
264-
{
265-
// A bit dirty
266-
MysqlUser::executeSchema();
267-
MysqlBook::executeSchema();
268-
MysqlRole::executeSchema();
269-
270-
$user = new MysqlUser;
271-
$this->assertInstanceOf('MysqlUser', $user);
272-
$this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
273-
274-
// Mysql User
275-
$user->name = "John Doe";
276-
$user->save();
277-
$this->assertTrue(is_int($user->id));
278-
279-
// SQL has many
280-
$book = new Book(array('title' => 'Game of Thrones'));
281-
$user->books()->save($book);
282-
$user = MysqlUser::find($user->id); // refetch
283-
$this->assertEquals(1, count($user->books));
284-
285-
// MongoDB belongs to
286-
$book = $user->books()->first(); // refetch
287-
$this->assertEquals('John Doe', $book->mysqlAuthor->name);
288-
289-
// SQL has one
290-
$role = new Role(array('type' => 'admin'));
291-
$user->role()->save($role);
292-
$user = MysqlUser::find($user->id); // refetch
293-
$this->assertEquals('admin', $user->role->type);
294-
295-
// MongoDB beelongs to
296-
$role = $user->role()->first(); // refetch
297-
$this->assertEquals('John Doe', $role->mysqlUser->name);
298-
299-
// MongoDB User
300-
$user = new User;
301-
$user->name = "John Doe";
302-
$user->save();
303-
304-
// MongoDB has many
305-
$book = new MysqlBook(array('title' => 'Game of Thrones'));
306-
$user->mysqlBooks()->save($book);
307-
$user = User::find($user->_id); // refetch
308-
$this->assertEquals(1, count($user->mysqlBooks));
309-
310-
// SQL belongs to
311-
$book = $user->mysqlBooks()->first(); // refetch
312-
$this->assertEquals('John Doe', $book->author->name);
313-
314-
// MongoDB has one
315-
$role = new MysqlRole(array('type' => 'admin'));
316-
$user->mysqlRole()->save($role);
317-
$user = User::find($user->_id); // refetch
318-
$this->assertEquals('admin', $user->mysqlRole->type);
319-
320-
// SQL belongs to
321-
$role = $user->mysqlRole()->first(); // refetch
322-
$this->assertEquals('John Doe', $role->user->name);
323-
324-
// Dirty again :)
325-
MysqlUser::truncate();
326-
MysqlBook::truncate();
327-
MysqlRole::truncate();
328-
}
329262
}

0 commit comments

Comments
 (0)