Commit 94314570 authored by Jens Segers's avatar Jens Segers

Tweak belongsToMany relation

parent 12abfb36
<?php namespace Jenssegers\Mongodb\Relations; <?php namespace Jenssegers\Mongodb\Relations;
use Jenssegers\Mongodb\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
...@@ -40,6 +40,45 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -40,6 +40,45 @@ class BelongsToMany extends EloquentBelongsToMany {
} }
} }
/**
* Save a new model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public function save(Model $model, array $joining = array(), $touch = true)
{
$model->save(array('touch' => false));
$this->attach($model, $joining, $touch);
return $model;
}
/**
* Create a new instance of the related model.
*
* @param array $attributes
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $attributes, array $joining = array(), $touch = true)
{
$instance = $this->related->newInstance($attributes);
// Once we save the related model, we need to attach it to the base model via
// through intermediate table so we'll use the existing "attach" method to
// accomplish this which will insert the record and any more attributes.
$instance->save(array('touch' => false));
$this->attach($instance, $joining, $touch);
return $instance;
}
/** /**
* Sync the intermediate tables with a list of IDs or collection of models. * Sync the intermediate tables with a list of IDs or collection of models.
* *
...@@ -102,7 +141,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -102,7 +141,7 @@ class BelongsToMany extends EloquentBelongsToMany {
*/ */
public function updateExistingPivot($id, array $attributes, $touch = true) public function updateExistingPivot($id, array $attributes, $touch = true)
{ {
// TODO // Do nothing, we have no pivot table.
} }
/** /**
...@@ -115,7 +154,10 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -115,7 +154,10 @@ class BelongsToMany extends EloquentBelongsToMany {
*/ */
public function attach($id, array $attributes = array(), $touch = true) public function attach($id, array $attributes = array(), $touch = true)
{ {
if ($id instanceof Model) $id = $id->getKey(); if ($id instanceof Model)
{
$model = $id; $id = $model->getKey();
}
$records = $this->createAttachRecords((array) $id, $attributes); $records = $this->createAttachRecords((array) $id, $attributes);
...@@ -126,14 +168,23 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -126,14 +168,23 @@ class BelongsToMany extends EloquentBelongsToMany {
// Attach the new ids to the parent model. // Attach the new ids to the parent model.
$this->parent->push($this->otherKey, $otherIds, true); $this->parent->push($this->otherKey, $otherIds, true);
// Generate a new related query instance. // If we have a model instance, we can psuh the ids to that model,
$query = $this->newRelatedQuery(); // so that the internal attributes are updated as well. Otherwise,
// we will just perform a regular database query.
if (isset($model))
{
// Attach the new ids to the related model.
$model->push($this->foreignKey, $foreignIds, true);
}
else
{
$query = $this->newRelatedQuery();
// Set contraints on the related query. $query->where($this->related->getKeyName(), $id);
$query->where($this->related->getKeyName(), $id);
// Attach the new ids to the related model. // Attach the new ids to the related model.
$query->push($this->foreignKey, $foreignIds, true); $query->push($this->foreignKey, $foreignIds, true);
}
if ($touch) $this->touchIfTouching(); if ($touch) $this->touchIfTouching();
} }
......
...@@ -455,20 +455,20 @@ class RelationsTest extends TestCase { ...@@ -455,20 +455,20 @@ class RelationsTest extends TestCase {
$user->save(); $user->save();
$this->assertEquals(1, $user->clients()->count()); $this->assertEquals(1, $user->clients()->count());
//$this->assertEquals(array($user->_id), $client->user_ids); TODO $this->assertEquals(array($user->_id), $client->user_ids);
$this->assertEquals(array($client->_id), $user->client_ids); $this->assertEquals(array($client->_id), $user->client_ids);
$user = User::where('name', 'John Doe')->first(); $user = User::where('name', 'John Doe')->first();
$client = Client::where('name', 'Admins')->first(); $client = Client::where('name', 'Admins')->first();
$this->assertEquals(1, $user->clients()->count()); $this->assertEquals(1, $user->clients()->count());
//$this->assertEquals(array($user->_id), $client->user_ids); TODO $this->assertEquals(array($user->_id), $client->user_ids);
$this->assertEquals(array($client->_id), $user->client_ids); $this->assertEquals(array($client->_id), $user->client_ids);
$user->clients()->save($client); $user->clients()->save($client);
$user->clients()->save($client); $user->clients()->save($client);
$user->save(); $user->save();
$this->assertEquals(1, $user->clients()->count()); $this->assertEquals(1, $user->clients()->count());
//$this->assertEquals(array($user->_id), $client->user_ids); TODO $this->assertEquals(array($user->_id), $client->user_ids);
$this->assertEquals(array($client->_id), $user->client_ids); $this->assertEquals(array($client->_id), $user->client_ids);
} }
......
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