Commit a54ef6d9 authored by Jens Segers's avatar Jens Segers

Merge branch 'master' into develop

parents e647782f 9c8bf642
......@@ -57,16 +57,6 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
{
if (is_null($localKey))
{
$localKey = snake_case(str_plural($related)) . '_ids';
}
if (is_null($foreignKey))
{
$foreignKey = snake_case(class_basename($this));
}
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
......@@ -77,6 +67,16 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
$relation = $caller['function'];
}
if (is_null($localKey))
{
$localKey = '_' . $relation;
}
if (is_null($foreignKey))
{
$foreignKey = snake_case(class_basename($this));
}
$query = $this->newQuery();
$instance = new $related;
......
......@@ -141,7 +141,7 @@ class EmbedsMany extends Relation {
public function save(Model $model)
{
// Insert a new document.
if (!$model->exists)
if ( ! $model->exists)
{
return $this->performInsert($model);
}
......@@ -162,7 +162,7 @@ class EmbedsMany extends Relation {
protected function performInsert(Model $model)
{
// Create a new key.
if (!isset($model['_id']))
if ( ! $model->getAttribute('_id'))
{
$model->setAttribute('_id', new MongoId);
}
......@@ -265,12 +265,7 @@ class EmbedsMany extends Relation {
*/
public function createMany(array $records)
{
$instances = array();
foreach ($records as $record)
{
$instances[] = $this->create($record);
}
$instances = array_map(array($this, 'create'), $records);
return $instances;
}
......
......@@ -104,8 +104,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$all = User::all();
$this->assertEquals(2, count($all));
$this->assertEquals('John Doe', $all[0]->name);
$this->assertEquals('Jane Doe', $all[1]->name);
$this->assertContains('John Doe', $all->lists('name'));
$this->assertContains('Jane Doe', $all->lists('name'));
}
public function testFind()
......
......@@ -78,10 +78,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$users = DB::collection('users')->get();
$this->assertEquals(2, count($users));
$user = $users[0];
$this->assertEquals('Jane Doe', $user['name']);
$this->assertTrue(is_array($user['tags']));
$this->assertTrue(is_array($users[0]['tags']));
}
public function testFind()
......@@ -118,8 +115,10 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100));
$users = DB::collection('users')->get();
$this->assertEquals(20, $users[0]['age']);
$this->assertEquals(100, $users[1]['age']);
$john = DB::collection('users')->where('name', 'John Doe')->first();
$jane = DB::collection('users')->where('name', 'Jane Doe')->first();
$this->assertEquals(100, $john['age']);
$this->assertEquals(20, $jane['age']);
}
public function testDelete()
......@@ -312,9 +311,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
));
$items = DB::collection('items')->take(2)->get();
$items = DB::collection('items')->orderBy('name')->take(2)->get();
$this->assertEquals(2, count($items));
$this->assertEquals('knife', $items[0]['name']);
$this->assertEquals('fork', $items[0]['name']);
}
public function testSkip()
......@@ -326,7 +325,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
));
$items = DB::collection('items')->skip(2)->get();
$items = DB::collection('items')->orderBy('name')->skip(2)->get();
$this->assertEquals(2, count($items));
$this->assertEquals('spoon', $items[0]['name']);
}
......@@ -352,8 +351,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
));
$list = DB::collection('items')->lists('name');
sort($list);
$this->assertEquals(4, count($list));
$this->assertEquals(array('knife', 'fork', 'spoon', 'spoon'), $list);
$this->assertEquals(array('fork', 'knife', 'spoon', 'spoon'), $list);
$list = DB::collection('items')->lists('type', 'name');
$this->assertEquals(3, count($list));
......@@ -470,7 +470,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$results = DB::collection('users')->where('age', 'exists', true)->get();
$this->assertEquals(2, count($results));
$this->assertEquals('John Doe', $results[0]['name']);
$resultsNames = array($results[0]['name'], $results[1]['name']);
$this->assertContains('John Doe', $resultsNames);
$this->assertContains('Robert Roe', $resultsNames);
$results = DB::collection('users')->where('age', 'exists', false)->get();
$this->assertEquals(1, count($results));
......
......@@ -70,18 +70,18 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public function testSelect()
{
$user = User::select('name')->first();
$user = User::where('name', 'John Doe')->select('name')->first();
$this->assertEquals('John Doe', $user->name);
$this->assertEquals(null, $user->age);
$user = User::select('name', 'title')->first();
$user = User::where('name', 'John Doe')->select('name', 'title')->first();
$this->assertEquals('John Doe', $user->name);
$this->assertEquals('admin', $user->title);
$this->assertEquals(null, $user->age);
$user = User::get(array('name'))->first();
$user = User::where('name', 'John Doe')->get(array('name'))->first();
$this->assertEquals('John Doe', $user->name);
$this->assertEquals(null, $user->age);
......
......@@ -146,13 +146,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
$clients = $client->getRelation('users');
$users = $user->getRelation('clients');
$users = $client->getRelation('users');
$clients = $user->getRelation('clients');
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
$this->assertInstanceOf('Client', $users[0]);
$this->assertInstanceOf('User', $clients[0]);
$this->assertInstanceOf('Client', $clients[0]);
$this->assertInstanceOf('User', $users[0]);
$this->assertCount(2, $user->clients);
$this->assertCount(1, $client->users);
......@@ -289,6 +289,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$address = new Address(array('city' => 'London'));
$address = $user->addresses()->save($address);
$this->assertNotNull($user->_addresses);
$this->assertEquals(array('London'), $user->addresses->lists('city'));
$this->assertInstanceOf('DateTime', $address->created_at);
$this->assertInstanceOf('DateTime', $address->updated_at);
......@@ -340,12 +341,30 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
public function testEmbedsManyCreate()
{
$user = User::create(array('name' => 'John Doe'));
$user->addresses()->create(array('city' => 'Bruxelles'));
$user = User::create(array());
$address = $user->addresses()->create(array('city' => 'Bruxelles'));
$this->assertInstanceOf('Address', $address);
$this->assertInstanceOf('MongoID', $address->_id);
$this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
$freshUser = User::find($user->id);
$this->assertEquals(array('Bruxelles'), $freshUser->addresses->lists('city'));
$user = User::create(array());
$address = $user->addresses()->create(array('_id' => '', 'city' => 'Bruxelles'));
$this->assertInstanceOf('MongoID', $address->_id);
}
public function testEmbedsManyCreateMany()
{
$user = User::create(array());
list($bruxelles, $paris) = $user->addresses()->createMany(array(array('city' => 'Bruxelles'), array('city' => 'Paris')));
$this->assertInstanceOf('Address', $bruxelles);
$this->assertEquals('Bruxelles', $bruxelles->city);
$this->assertEquals(array('Bruxelles', 'Paris'), $user->addresses->lists('city'));
$freshUser = User::find($user->id);
$this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city'));
}
public function testEmbedsManyDestroy()
......
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