Skip to content

[5.0] The findOrFail method should throw an error when any model is missing #7048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,22 @@ public function findMany($id, $columns = array('*'))
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|static
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function findOrFail($id, $columns = array('*'))
{
if ( ! is_null($model = $this->find($id, $columns))) return $model;
$result = $this->find($id, $columns);

if (is_array($id))
{
if (count($result) == count(array_unique($id))) return $result;
}
elseif ( ! is_null($result))
{
return $result;
}

throw (new ModelNotFoundException)->setModel(get_class($this->model));
}
Expand Down
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,7 @@ public static function findOrNew($id, $columns = array('*'))
*/
public static function findOrFail($id, $columns = array('*'))
{
if ( ! is_null($model = static::find($id, $columns))) return $model;

throw (new ModelNotFoundException)->setModel(get_called_class());
return static::query()->findOrFail($id, $columns);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public function testFindOrFailMethodThrowsModelNotFoundException()
$result = $builder->findOrFail('bar', array('column'));
}

/**
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindOrFailMethodWithManyThrowsModelNotFoundException()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[get]', array($this->getMockQueryBuilder()));
$builder->setModel($this->getMockModel());
$builder->getQuery()->shouldReceive('whereIn')->once()->with('foo_table.foo', [1, 2]);
$builder->shouldReceive('get')->with(array('column'))->andReturn(new Collection([1]));
$result = $builder->findOrFail([1, 2], array('column'));
}

/**
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
*/
Expand Down Expand Up @@ -437,7 +449,7 @@ public function testHasNestedWithConstraints()
$this->assertEquals($builder, $result);
}


public function testHasNested()
{
$model = new EloquentBuilderTestModelParentStub;
Expand Down
5 changes: 3 additions & 2 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Mockery as m;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class DatabaseEloquentModelTest extends PHPUnit_Framework_TestCase {

Expand Down Expand Up @@ -1263,10 +1264,10 @@ public function newQuery()
}

class EloquentModelFindNotFoundStub extends Illuminate\Database\Eloquent\Model {
public function newQuery()
public static function query()
{
$mock = m::mock('Illuminate\Database\Eloquent\Builder');
$mock->shouldReceive('find')->once()->with(1, array('*'))->andReturn(null);
$mock->shouldReceive('findOrFail')->once()->with(1, array('*'))->andThrow(new ModelNotFoundException);
return $mock;
}
}
Expand Down