Commit 3058f37a authored by Jens Segers's avatar Jens Segers

Rename getParent to getParentRelation and added more tests for soft deletion

parent d5283da2
...@@ -26,7 +26,7 @@ class Builder extends EloquentBuilder { ...@@ -26,7 +26,7 @@ class Builder extends EloquentBuilder {
{ {
// Intercept operations on embedded models and delegate logic // Intercept operations on embedded models and delegate logic
// to the parent relation instance. // to the parent relation instance.
if ($relation = $this->model->getParent()) if ($relation = $this->model->getParentRelation())
{ {
$relation->performUpdate($this->model, $values); $relation->performUpdate($this->model, $values);
...@@ -46,7 +46,7 @@ class Builder extends EloquentBuilder { ...@@ -46,7 +46,7 @@ class Builder extends EloquentBuilder {
{ {
// Intercept operations on embedded models and delegate logic // Intercept operations on embedded models and delegate logic
// to the parent relation instance. // to the parent relation instance.
if ($relation = $this->model->getParent()) if ($relation = $this->model->getParentRelation())
{ {
$relation->performInsert($this->model, $values); $relation->performInsert($this->model, $values);
...@@ -67,7 +67,7 @@ class Builder extends EloquentBuilder { ...@@ -67,7 +67,7 @@ class Builder extends EloquentBuilder {
{ {
// Intercept operations on embedded models and delegate logic // Intercept operations on embedded models and delegate logic
// to the parent relation instance. // to the parent relation instance.
if ($relation = $this->model->getParent()) if ($relation = $this->model->getParentRelation())
{ {
$relation->performInsert($this->model, $values); $relation->performInsert($this->model, $values);
...@@ -86,7 +86,7 @@ class Builder extends EloquentBuilder { ...@@ -86,7 +86,7 @@ class Builder extends EloquentBuilder {
{ {
// Intercept operations on embedded models and delegate logic // Intercept operations on embedded models and delegate logic
// to the parent relation instance. // to the parent relation instance.
if ($relation = $this->model->getParent()) if ($relation = $this->model->getParentRelation())
{ {
$relation->performDelete($this->model); $relation->performDelete($this->model);
...@@ -108,7 +108,7 @@ class Builder extends EloquentBuilder { ...@@ -108,7 +108,7 @@ class Builder extends EloquentBuilder {
{ {
// Intercept operations on embedded models and delegate logic // Intercept operations on embedded models and delegate logic
// to the parent relation instance. // to the parent relation instance.
if ($relation = $this->model->getParent()) if ($relation = $this->model->getParentRelation())
{ {
$value = $this->model->{$column}; $value = $this->model->{$column};
...@@ -138,7 +138,7 @@ class Builder extends EloquentBuilder { ...@@ -138,7 +138,7 @@ class Builder extends EloquentBuilder {
{ {
// Intercept operations on embedded models and delegate logic // Intercept operations on embedded models and delegate logic
// to the parent relation instance. // to the parent relation instance.
if ($relation = $this->model->getParent()) if ($relation = $this->model->getParentRelation())
{ {
$value = $this->model->{$column}; $value = $this->model->{$column};
......
...@@ -35,7 +35,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -35,7 +35,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
* *
* @var Relation * @var Relation
*/ */
protected $parent; protected $parentRelation;
/** /**
* The connection resolver instance. * The connection resolver instance.
...@@ -470,9 +470,9 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -470,9 +470,9 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
* *
* @param Relation $relation * @param Relation $relation
*/ */
public function setParent(Relation $relation) public function setParentRelation(Relation $relation)
{ {
$this->parent = $relation; $this->parentRelation = $relation;
} }
/** /**
...@@ -480,9 +480,9 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -480,9 +480,9 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
* *
* @return Relation * @return Relation
*/ */
public function getParent() public function getParentRelation()
{ {
return $this->parent; return $this->parentRelation;
} }
/** /**
......
...@@ -94,7 +94,7 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -94,7 +94,7 @@ abstract class EmbedsOneOrMany extends Relation {
{ {
foreach ($models as $model) foreach ($models as $model)
{ {
$model->setParent($this); $model->setParentRelation($this);
$model->setRelation($relation, $this->related->newCollection()); $model->setRelation($relation, $this->related->newCollection());
} }
...@@ -116,7 +116,7 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -116,7 +116,7 @@ abstract class EmbedsOneOrMany extends Relation {
{ {
$results = $model->$relation()->getResults(); $results = $model->$relation()->getResults();
$model->setParent($this); $model->setParentRelation($this);
$model->setRelation($relation, $results); $model->setRelation($relation, $results);
} }
...@@ -152,7 +152,7 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -152,7 +152,7 @@ abstract class EmbedsOneOrMany extends Relation {
*/ */
public function save(Model $model) public function save(Model $model)
{ {
$model->setParent($this); $model->setParentRelation($this);
return $model->save() ? $model : false; return $model->save() ? $model : false;
} }
...@@ -183,7 +183,7 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -183,7 +183,7 @@ abstract class EmbedsOneOrMany extends Relation {
// on the models. Otherwise, some of these attributes will not get set. // on the models. Otherwise, some of these attributes will not get set.
$instance = $this->related->newInstance($attributes); $instance = $this->related->newInstance($attributes);
$instance->setParent($this); $instance->setParentRelation($this);
$instance->save(); $instance->save();
...@@ -310,7 +310,7 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -310,7 +310,7 @@ abstract class EmbedsOneOrMany extends Relation {
$model = $this->related->newFromBuilder((array) $attributes); $model = $this->related->newFromBuilder((array) $attributes);
$model->setParent($this); $model->setParentRelation($this);
$model->setRelation($this->foreignKey, $this->parent); $model->setRelation($this->foreignKey, $this->parent);
...@@ -327,7 +327,7 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -327,7 +327,7 @@ abstract class EmbedsOneOrMany extends Relation {
*/ */
protected function getParentRelation() protected function getParentRelation()
{ {
return $this->parent->getParent(); return $this->parent->getParentRelation();
} }
/** /**
......
...@@ -267,29 +267,33 @@ class ModelTest extends TestCase { ...@@ -267,29 +267,33 @@ class ModelTest extends TestCase {
public function testSoftDelete() public function testSoftDelete()
{ {
$user = new Soft; Soft::create(array('name' => 'John Doe'));
$user->name = 'Softy'; Soft::create(array('name' => 'Jane Doe'));
$user->save();
$this->assertEquals(2, Soft::count());
$user = Soft::where('name', 'John Doe')->first();
$this->assertEquals(true, $user->exists); $this->assertEquals(true, $user->exists);
$this->assertEquals(false, $user->trashed());
$this->assertNull($user->deleted_at);
$user->delete(); $user->delete();
$this->assertEquals(true, $user->trashed());
$this->assertNotNull($user->deleted_at);
$check = Soft::find($user->_id); $user = Soft::where('name', 'John Doe')->first();
$this->assertEquals(null, $check); $this->assertNull($user);
$all = Soft::get();
$this->assertEquals(0, $all->count());
$all = Soft::withTrashed()->get(); $this->assertEquals(1, Soft::count());
$this->assertEquals(1, $all->count()); $this->assertEquals(2, Soft::withTrashed()->count());
$check = $all[0]; $user = Soft::withTrashed()->where('name', 'John Doe')->first();
$this->assertInstanceOf('Carbon\Carbon', $check->deleted_at); $this->assertNotNull($user);
$this->assertEquals(true, $check->trashed()); $this->assertInstanceOf('Carbon\Carbon', $user->deleted_at);
$this->assertEquals(true, $user->trashed());
$check->restore(); $user->restore();
$all = Soft::get(); $this->assertEquals(2, Soft::count());
$this->assertEquals(1, $all->count());
} }
public function testPrimaryKey() public function testPrimaryKey()
......
...@@ -5,9 +5,10 @@ use Jenssegers\Mongodb\Eloquent\SoftDeletingTrait; ...@@ -5,9 +5,10 @@ use Jenssegers\Mongodb\Eloquent\SoftDeletingTrait;
class Soft extends Eloquent { class Soft extends Eloquent {
use SoftDeletingTrait; use SoftDeletingTrait;
protected $collection = 'soft'; protected $collection = 'soft';
protected $dates = array('deleted_at'); protected static $unguarded = true;
protected $dates = array('deleted_at');
} }
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