diff --git a/src/ORM/DomainChecker.php b/src/ORM/DomainChecker.php index 61c91f3b0f4..b9e230be483 100644 --- a/src/ORM/DomainChecker.php +++ b/src/ORM/DomainChecker.php @@ -28,6 +28,12 @@ class DomainChecker { protected $_updateRules = []; + protected $_options = []; + + public function __construct(array $options) { + $this->_options = $options; + } + public function add(callable $rule) { $this->_rules[] = $rule; return $this; @@ -46,7 +52,7 @@ public function addUpdate(callable $rule) { public function checkCreate(EntityInterface $entity) { $success = true; foreach (array_merge($this->_rules, $this->_createRules) as $rule) { - $success = $rule($entity) && $success; + $success = $rule($entity, $this->_options) && $success; } return $success; } @@ -54,7 +60,7 @@ public function checkCreate(EntityInterface $entity) { public function checkUpdate(EntityInterface $entity) { $success = true; foreach (array_merge($this->_rules, $this->_updateRules) as $rule) { - $success = $rule($entity) && $success; + $success = $rule($entity, $this->_options) && $success; } return $success; } diff --git a/src/ORM/Table.php b/src/ORM/Table.php index 7d37903739e..05fd16613ca 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -1856,7 +1856,7 @@ public function domainRules() { if ($this->_domainChecker !== null) { return $this->_domainChecker; } - return $this->_domainChecker = $this->buildDomainRules(new DomainChecker); + return $this->_domainChecker = $this->buildDomainRules(new DomainChecker(['scope' => $this])); } public function buildDomainRules(DomainChecker $rules) { diff --git a/tests/TestCase/ORM/DomainRulesIntegrationTest.php b/tests/TestCase/ORM/DomainRulesIntegrationTest.php index fcf1a62f6df..8c2adef523f 100644 --- a/tests/TestCase/ORM/DomainRulesIntegrationTest.php +++ b/tests/TestCase/ORM/DomainRulesIntegrationTest.php @@ -60,7 +60,8 @@ public function testsSaveBelongsToWithValidationError() { $table->association('authors') ->target() ->domainRules() - ->add(function (Entity $author) { + ->add(function (Entity $author, array $options) use ($table) { + $this->assertSame($options['scope'], $table->association('authors')->target()); $author->errors('name', ['This is an error']); return false; });