Commit 64b85438 authored by Jens Segers's avatar Jens Segers

Adding id accessor

parent 287d2c7e
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
<testsuites> <testsuites>
<testsuite name="all"> <testsuite name="all">
<directory>tests/</directory> <directory>tests/</directory>
<exclude>tests/MysqlRelationsTest.php</exclude>
</testsuite> </testsuite>
<testsuite name="schema"> <testsuite name="schema">
<directory>tests/SchemaTest.php</directory> <directory>tests/SchemaTest.php</directory>
...@@ -33,6 +34,7 @@ ...@@ -33,6 +34,7 @@
</testsuite> </testsuite>
<testsuite name="relations"> <testsuite name="relations">
<directory>tests/RelationsTest.php</directory> <directory>tests/RelationsTest.php</directory>
<directory>tests/MysqlRelationsTest.php</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>
...@@ -37,6 +37,19 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -37,6 +37,19 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/ */
protected static $resolver; protected static $resolver;
/**
* Custom accessor for the model's id.
*
* @return string
*/
public function getIdAttribute($value)
{
// If there is an actual id attribute, then return that.
if ($value) return $value;
return $this->getKey();
}
/** /**
* Convert a DateTime to a storable MongoDate object. * Convert a DateTime to a storable MongoDate object.
* *
...@@ -132,7 +145,10 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -132,7 +145,10 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
{ {
foreach($attributes as $key => &$value) foreach($attributes as $key => &$value)
{ {
// Convert MongoId to string /**
* MongoIds are converted to string to make it easier to pass
* the id to other instances or relations.
*/
if ($value instanceof MongoId) if ($value instanceof MongoId)
{ {
$value = (string) $value; $value = (string) $value;
...@@ -153,7 +169,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -153,7 +169,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
foreach ($attributes as &$value) foreach ($attributes as &$value)
{ {
// Convert MongoDate to string /**
* Here we convert MongoDate instances to string. This mimics
* original SQL behaviour so that dates are formatted nicely
* when your models are converted to JSON.
*/
if ($value instanceof MongoDate) if ($value instanceof MongoDate)
{ {
$value = $this->asDateTime($value)->format('Y-m-d H:i:s'); $value = $this->asDateTime($value)->format('Y-m-d H:i:s');
......
...@@ -334,4 +334,13 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -334,4 +334,13 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('John Doe', $user->name); $this->assertEquals('John Doe', $user->name);
} }
public function testIdAttribute()
{
$user = User::create(array('name' => 'John Doe'));
$this->assertEquals($user->id, $user->_id);
$user = User::create(array('id' => 'custom_id', 'name' => 'John Doe'));
$this->assertNotEquals($user->id, $user->_id);
}
} }
<?php
class MysqlRelationsTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
MysqlUser::executeSchema();
MysqlBook::executeSchema();
MysqlRole::executeSchema();
}
public function tearDown()
{
MysqlUser::truncate();
MysqlBook::truncate();
MysqlRole::truncate();
}
public function testMysqlRelations()
{
$user = new MysqlUser;
$this->assertInstanceOf('MysqlUser', $user);
$this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
// Mysql User
$user->name = "John Doe";
$user->save();
$this->assertTrue(is_int($user->id));
// SQL has many
$book = new Book(array('title' => 'Game of Thrones'));
$user->books()->save($book);
$user = MysqlUser::find($user->id); // refetch
$this->assertEquals(1, count($user->books));
// MongoDB belongs to
$book = $user->books()->first(); // refetch
$this->assertEquals('John Doe', $book->mysqlAuthor->name);
// SQL has one
$role = new Role(array('type' => 'admin'));
$user->role()->save($role);
$user = MysqlUser::find($user->id); // refetch
$this->assertEquals('admin', $user->role->type);
// MongoDB beelongs to
$role = $user->role()->first(); // refetch
$this->assertEquals('John Doe', $role->mysqlUser->name);
// MongoDB User
$user = new User;
$user->name = "John Doe";
$user->save();
// MongoDB has many
$book = new MysqlBook(array('title' => 'Game of Thrones'));
$user->mysqlBooks()->save($book);
$user = User::find($user->_id); // refetch
$this->assertEquals(1, count($user->mysqlBooks));
// SQL belongs to
$book = $user->mysqlBooks()->first(); // refetch
$this->assertEquals('John Doe', $book->author->name);
// MongoDB has one
$role = new MysqlRole(array('type' => 'admin'));
$user->mysqlRole()->save($role);
$user = User::find($user->_id); // refetch
$this->assertEquals('admin', $user->mysqlRole->type);
// SQL belongs to
$role = $user->mysqlRole()->first(); // refetch
$this->assertEquals('John Doe', $role->user->name);
}
}
...@@ -259,71 +259,4 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -259,71 +259,4 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($group->_id, $user->groups()->first()->_id); $this->assertEquals($group->_id, $user->groups()->first()->_id);
$this->assertEquals($user->_id, $group->users()->first()->_id); $this->assertEquals($user->_id, $group->users()->first()->_id);
} }
public function testMysqlModel()
{
// A bit dirty
MysqlUser::executeSchema();
MysqlBook::executeSchema();
MysqlRole::executeSchema();
$user = new MysqlUser;
$this->assertInstanceOf('MysqlUser', $user);
$this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
// Mysql User
$user->name = "John Doe";
$user->save();
$this->assertTrue(is_int($user->id));
// SQL has many
$book = new Book(array('title' => 'Game of Thrones'));
$user->books()->save($book);
$user = MysqlUser::find($user->id); // refetch
$this->assertEquals(1, count($user->books));
// MongoDB belongs to
$book = $user->books()->first(); // refetch
$this->assertEquals('John Doe', $book->mysqlAuthor->name);
// SQL has one
$role = new Role(array('type' => 'admin'));
$user->role()->save($role);
$user = MysqlUser::find($user->id); // refetch
$this->assertEquals('admin', $user->role->type);
// MongoDB beelongs to
$role = $user->role()->first(); // refetch
$this->assertEquals('John Doe', $role->mysqlUser->name);
// MongoDB User
$user = new User;
$user->name = "John Doe";
$user->save();
// MongoDB has many
$book = new MysqlBook(array('title' => 'Game of Thrones'));
$user->mysqlBooks()->save($book);
$user = User::find($user->_id); // refetch
$this->assertEquals(1, count($user->mysqlBooks));
// SQL belongs to
$book = $user->mysqlBooks()->first(); // refetch
$this->assertEquals('John Doe', $book->author->name);
// MongoDB has one
$role = new MysqlRole(array('type' => 'admin'));
$user->mysqlRole()->save($role);
$user = User::find($user->_id); // refetch
$this->assertEquals('admin', $user->mysqlRole->type);
// SQL belongs to
$role = $user->mysqlRole()->first(); // refetch
$this->assertEquals('John Doe', $role->user->name);
// Dirty again :)
MysqlUser::truncate();
MysqlBook::truncate();
MysqlRole::truncate();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment