Skip to content
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

[5.6] Ignore a given model during a unique check. #23524

Merged
merged 3 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 25 additions & 4 deletions src/Illuminate/Validation/Rules/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Validation\Rules;

use Illuminate\Database\Eloquent\Model;

class Unique
{
use DatabaseRule;
Expand All @@ -18,7 +20,7 @@ class Unique
*
* @var string
*/
protected $idColumn = 'id';
protected $idColumn;

/**
* Ignore the given ID during the unique check.
Expand All @@ -27,10 +29,29 @@ class Unique
* @param string $idColumn
* @return $this
*/
public function ignore($id, $idColumn = 'id')
public function ignore($id, $idColumn = null)
{
if ($id instanceof Model) {
return $this->ignoreModel($id, $idColumn);
}

$this->ignore = $id;
$this->idColumn = $idColumn;
$this->idColumn = $idColumn ?? 'id';

return $this;
}

/**
* Ignore the given model during the unique check.
*
* @param Model $model
* @param string|null $idColumn
* @return $this
*/
public function ignoreModel($model, $idColumn = null)
{
$this->idColumn = $idColumn ?? $model->getKeyName();
$this->ignore = $model->{$this->idColumn};

return $this;
}
Expand All @@ -46,7 +67,7 @@ public function __toString()
$this->table,
$this->column,
$this->ignore ? '"'.$this->ignore.'"' : 'NULL',
$this->idColumn,
$this->idColumn ?? 'NULL',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we removed the default value for the protected $idColumn; and the __toString() need string conversion for the variables

$this->formatWheres()
), ',');
}
Expand Down
1 change: 1 addition & 0 deletions tests/Support/framework/testing/disks/testing/letter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

@Alymosul Alymosul Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, it was wrongly added. Sorry for that.

21 changes: 20 additions & 1 deletion tests/Validation/ValidationUniqueRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace Illuminate\Tests\Validation;

use PHPUnit\Framework\TestCase;
use Illuminate\Database\Eloquent\Model;

class ValidationUniqueRuleTest extends TestCase
{
public function testItCorrectlyFormatsAStringVersionOfTheRule()
{
$rule = new \Illuminate\Validation\Rules\Unique('table');
$rule->where('foo', 'bar');
$this->assertEquals('unique:table,NULL,NULL,id,foo,bar', (string) $rule);
$this->assertEquals('unique:table,NULL,NULL,NULL,foo,bar', (string) $rule);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me uncomfortable having to change an old test to add this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need the default value for the idColumn that's why i had to change the test here, because we really don't need the 'id' since the ignore is NULL. but I can revert this change and still the feature will pass.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK please don't change that test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Sorry for that 👍


$rule = new \Illuminate\Validation\Rules\Unique('table', 'column');
$rule->ignore('Taylor, Otwell', 'id_column');
Expand All @@ -21,5 +22,23 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
$rule->ignore(null, 'id_column');
$rule->where('foo', 'bar');
$this->assertEquals('unique:table,column,NULL,id_column,foo,bar', (string) $rule);

$model = new EloquentModelStub(['id_column' => 1]);

$rule = new \Illuminate\Validation\Rules\Unique('table', 'column');
$rule->ignore($model);
$rule->where('foo', 'bar');
$this->assertEquals('unique:table,column,"1",id_column,foo,bar', (string) $rule);

$rule = new \Illuminate\Validation\Rules\Unique('table', 'column');
$rule->ignore($model, 'id_column');
$rule->where('foo', 'bar');
$this->assertEquals('unique:table,column,"1",id_column,foo,bar', (string) $rule);
}
}

class EloquentModelStub extends Model
{
protected $primaryKey = 'id_column';
protected $guarded = [];
}