Skip to content

Commit 7dd7e35

Browse files
committed
Merge pull request #7048 from JosephSilber/findOrFail-many
[5.0] The findOrFail method should throw an error when any model is missing
2 parents 2372851 + f109e48 commit 7dd7e35

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,22 @@ public function findMany($id, $columns = array('*'))
105105
*
106106
* @param mixed $id
107107
* @param array $columns
108-
* @return \Illuminate\Database\Eloquent\Model|static
108+
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
109109
*
110110
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
111111
*/
112112
public function findOrFail($id, $columns = array('*'))
113113
{
114-
if ( ! is_null($model = $this->find($id, $columns))) return $model;
114+
$result = $this->find($id, $columns);
115+
116+
if (is_array($id))
117+
{
118+
if (count($result) == count(array_unique($id))) return $result;
119+
}
120+
elseif ( ! is_null($result))
121+
{
122+
return $result;
123+
}
115124

116125
throw (new ModelNotFoundException)->setModel(get_class($this->model));
117126
}

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,7 @@ public static function findOrNew($id, $columns = array('*'))
725725
*/
726726
public static function findOrFail($id, $columns = array('*'))
727727
{
728-
if ( ! is_null($model = static::find($id, $columns))) return $model;
729-
730-
throw (new ModelNotFoundException)->setModel(get_called_class());
728+
return static::query()->findOrFail($id, $columns);
731729
}
732730

733731
/**

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public function testFindOrFailMethodThrowsModelNotFoundException()
6868
$result = $builder->findOrFail('bar', array('column'));
6969
}
7070

71+
/**
72+
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
73+
*/
74+
public function testFindOrFailMethodWithManyThrowsModelNotFoundException()
75+
{
76+
$builder = m::mock('Illuminate\Database\Eloquent\Builder[get]', array($this->getMockQueryBuilder()));
77+
$builder->setModel($this->getMockModel());
78+
$builder->getQuery()->shouldReceive('whereIn')->once()->with('foo_table.foo', [1, 2]);
79+
$builder->shouldReceive('get')->with(array('column'))->andReturn(new Collection([1]));
80+
$result = $builder->findOrFail([1, 2], array('column'));
81+
}
82+
7183
/**
7284
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
7385
*/
@@ -437,7 +449,7 @@ public function testHasNestedWithConstraints()
437449
$this->assertEquals($builder, $result);
438450
}
439451

440-
452+
441453
public function testHasNested()
442454
{
443455
$model = new EloquentBuilderTestModelParentStub;

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Mockery as m;
4+
use Illuminate\Database\Eloquent\ModelNotFoundException;
45

56
class DatabaseEloquentModelTest extends PHPUnit_Framework_TestCase {
67

@@ -1263,10 +1264,10 @@ public function newQuery()
12631264
}
12641265

12651266
class EloquentModelFindNotFoundStub extends Illuminate\Database\Eloquent\Model {
1266-
public function newQuery()
1267+
public static function query()
12671268
{
12681269
$mock = m::mock('Illuminate\Database\Eloquent\Builder');
1269-
$mock->shouldReceive('find')->once()->with(1, array('*'))->andReturn(null);
1270+
$mock->shouldReceive('findOrFail')->once()->with(1, array('*'))->andThrow(new ModelNotFoundException);
12701271
return $mock;
12711272
}
12721273
}

0 commit comments

Comments
 (0)