Skip to content

Commit 3058f37

Browse files
committed
Rename getParent to getParentRelation and added more tests for soft deletion
1 parent d5283da commit 3058f37

File tree

5 files changed

+41
-36
lines changed

5 files changed

+41
-36
lines changed

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function update(array $values)
2626
{
2727
// Intercept operations on embedded models and delegate logic
2828
// to the parent relation instance.
29-
if ($relation = $this->model->getParent())
29+
if ($relation = $this->model->getParentRelation())
3030
{
3131
$relation->performUpdate($this->model, $values);
3232

@@ -46,7 +46,7 @@ public function insert(array $values)
4646
{
4747
// Intercept operations on embedded models and delegate logic
4848
// to the parent relation instance.
49-
if ($relation = $this->model->getParent())
49+
if ($relation = $this->model->getParentRelation())
5050
{
5151
$relation->performInsert($this->model, $values);
5252

@@ -67,7 +67,7 @@ public function insertGetId(array $values, $sequence = null)
6767
{
6868
// Intercept operations on embedded models and delegate logic
6969
// to the parent relation instance.
70-
if ($relation = $this->model->getParent())
70+
if ($relation = $this->model->getParentRelation())
7171
{
7272
$relation->performInsert($this->model, $values);
7373

@@ -86,7 +86,7 @@ public function delete()
8686
{
8787
// Intercept operations on embedded models and delegate logic
8888
// to the parent relation instance.
89-
if ($relation = $this->model->getParent())
89+
if ($relation = $this->model->getParentRelation())
9090
{
9191
$relation->performDelete($this->model);
9292

@@ -108,7 +108,7 @@ public function increment($column, $amount = 1, array $extra = array())
108108
{
109109
// Intercept operations on embedded models and delegate logic
110110
// to the parent relation instance.
111-
if ($relation = $this->model->getParent())
111+
if ($relation = $this->model->getParentRelation())
112112
{
113113
$value = $this->model->{$column};
114114

@@ -138,7 +138,7 @@ public function decrement($column, $amount = 1, array $extra = array())
138138
{
139139
// Intercept operations on embedded models and delegate logic
140140
// to the parent relation instance.
141-
if ($relation = $this->model->getParent())
141+
if ($relation = $this->model->getParentRelation())
142142
{
143143
$value = $this->model->{$column};
144144

src/Jenssegers/Mongodb/Model.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
3535
*
3636
* @var Relation
3737
*/
38-
protected $parent;
38+
protected $parentRelation;
3939

4040
/**
4141
* The connection resolver instance.
@@ -470,19 +470,19 @@ protected function pullAttributeValues($column, array $values)
470470
*
471471
* @param Relation $relation
472472
*/
473-
public function setParent(Relation $relation)
473+
public function setParentRelation(Relation $relation)
474474
{
475-
$this->parent = $relation;
475+
$this->parentRelation = $relation;
476476
}
477477

478478
/**
479479
* Get the parent relation.
480480
*
481481
* @return Relation
482482
*/
483-
public function getParent()
483+
public function getParentRelation()
484484
{
485-
return $this->parent;
485+
return $this->parentRelation;
486486
}
487487

488488
/**

src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function initRelation(array $models, $relation)
9494
{
9595
foreach ($models as $model)
9696
{
97-
$model->setParent($this);
97+
$model->setParentRelation($this);
9898

9999
$model->setRelation($relation, $this->related->newCollection());
100100
}
@@ -116,7 +116,7 @@ public function match(array $models, BaseCollection $results, $relation)
116116
{
117117
$results = $model->$relation()->getResults();
118118

119-
$model->setParent($this);
119+
$model->setParentRelation($this);
120120

121121
$model->setRelation($relation, $results);
122122
}
@@ -152,7 +152,7 @@ public function count()
152152
*/
153153
public function save(Model $model)
154154
{
155-
$model->setParent($this);
155+
$model->setParentRelation($this);
156156

157157
return $model->save() ? $model : false;
158158
}
@@ -183,7 +183,7 @@ public function create(array $attributes)
183183
// on the models. Otherwise, some of these attributes will not get set.
184184
$instance = $this->related->newInstance($attributes);
185185

186-
$instance->setParent($this);
186+
$instance->setParentRelation($this);
187187

188188
$instance->save();
189189

@@ -310,7 +310,7 @@ protected function toModel($attributes = array())
310310

311311
$model = $this->related->newFromBuilder((array) $attributes);
312312

313-
$model->setParent($this);
313+
$model->setParentRelation($this);
314314

315315
$model->setRelation($this->foreignKey, $this->parent);
316316

@@ -327,7 +327,7 @@ protected function toModel($attributes = array())
327327
*/
328328
protected function getParentRelation()
329329
{
330-
return $this->parent->getParent();
330+
return $this->parent->getParentRelation();
331331
}
332332

333333
/**

tests/ModelTest.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -267,29 +267,33 @@ public function testTouch()
267267

268268
public function testSoftDelete()
269269
{
270-
$user = new Soft;
271-
$user->name = 'Softy';
272-
$user->save();
270+
Soft::create(array('name' => 'John Doe'));
271+
Soft::create(array('name' => 'Jane Doe'));
272+
273+
$this->assertEquals(2, Soft::count());
274+
275+
$user = Soft::where('name', 'John Doe')->first();
273276
$this->assertEquals(true, $user->exists);
277+
$this->assertEquals(false, $user->trashed());
278+
$this->assertNull($user->deleted_at);
274279

275280
$user->delete();
281+
$this->assertEquals(true, $user->trashed());
282+
$this->assertNotNull($user->deleted_at);
276283

277-
$check = Soft::find($user->_id);
278-
$this->assertEquals(null, $check);
279-
280-
$all = Soft::get();
281-
$this->assertEquals(0, $all->count());
284+
$user = Soft::where('name', 'John Doe')->first();
285+
$this->assertNull($user);
282286

283-
$all = Soft::withTrashed()->get();
284-
$this->assertEquals(1, $all->count());
287+
$this->assertEquals(1, Soft::count());
288+
$this->assertEquals(2, Soft::withTrashed()->count());
285289

286-
$check = $all[0];
287-
$this->assertInstanceOf('Carbon\Carbon', $check->deleted_at);
288-
$this->assertEquals(true, $check->trashed());
290+
$user = Soft::withTrashed()->where('name', 'John Doe')->first();
291+
$this->assertNotNull($user);
292+
$this->assertInstanceOf('Carbon\Carbon', $user->deleted_at);
293+
$this->assertEquals(true, $user->trashed());
289294

290-
$check->restore();
291-
$all = Soft::get();
292-
$this->assertEquals(1, $all->count());
295+
$user->restore();
296+
$this->assertEquals(2, Soft::count());
293297
}
294298

295299
public function testPrimaryKey()

tests/models/Soft.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
class Soft extends Eloquent {
77

8-
use SoftDeletingTrait;
8+
use SoftDeletingTrait;
99

10-
protected $collection = 'soft';
11-
protected $dates = array('deleted_at');
10+
protected $collection = 'soft';
11+
protected static $unguarded = true;
12+
protected $dates = array('deleted_at');
1213

1314
}

0 commit comments

Comments
 (0)