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