diff --git a/src/Datasource/EntityInterface.php b/src/Datasource/EntityInterface.php index c07aeb31761..c231f51a2cc 100644 --- a/src/Datasource/EntityInterface.php +++ b/src/Datasource/EntityInterface.php @@ -29,7 +29,7 @@ * @method array getVirtual() * @method $this setDirty($property, $isDirty) * @method bool isDirty($property = null) - * @method bool canBePersisted() + * @method bool hasErrors($includeHasErrorsFromNestedEntities = true) * @method array getErrors() * @method array getError($field) * @method array setErrors(array $fields, $overwrite = false) diff --git a/src/Datasource/EntityTrait.php b/src/Datasource/EntityTrait.php index 10159c92093..f489081d4d5 100644 --- a/src/Datasource/EntityTrait.php +++ b/src/Datasource/EntityTrait.php @@ -90,13 +90,6 @@ trait EntityTrait */ protected $_new = true; - /** - * Returns whether this entity can be persisted. - * - * @var bool - */ - protected $_canBePersisted = true; - /** * List of errors per field as stored in this object * @@ -865,7 +858,6 @@ public function clean() { $this->_dirty = []; $this->_errors = []; - $this->_canBePersisted = true; $this->_invalid = []; $this->_original = []; } @@ -900,13 +892,28 @@ public function isNew($new = null) } /** - * Returns whether this entity can be persisted. + * Returns whether this entity has errors. * + * @param bool $includeHasErrorsFromNestedEntities * @return bool */ - public function canBePersisted() + public function hasErrors($includeHasErrorsFromNestedEntities = true) { - return $this->_canBePersisted; + if (empty($this->_errors) === false) { + return true; + } + + if ($includeHasErrorsFromNestedEntities === false) { + return false; + } + + foreach ($this->_properties as $property) { + if ($this->_readHasErrors($property) === true) { + return true; + } + } + + return false; } /** @@ -961,7 +968,6 @@ public function getError($field) */ public function setErrors(array $fields, $overwrite = false) { - $this->_canBePersisted = false; if ($overwrite) { foreach ($fields as $f => $error) { $this->_errors[$f] = (array)$error; @@ -1109,6 +1115,29 @@ protected function _nestedErrors($field) return []; } + /** + * Reads if there are errors for one or many objects. + * + * @param array|\Cake\Datasource\EntityTrait $object The object to read errors from. + * @return bool + */ + protected function _readHasErrors($object) + { + if ($object instanceof EntityInterface && $object->hasErrors() === true) { + return true; + } + + if (is_array($object) === true) { + foreach ($object as $value) { + if ($this->_readHasErrors($value) === true) { + return true; + } + } + } + + return false; + } + /** * Read the error(s) from one or many objects. * @@ -1426,7 +1455,6 @@ public function __debugInfo() return $properties + [ '[new]' => $this->isNew(), '[accessible]' => $this->_accessible, - '[persistable]' => $this->_canBePersisted, '[dirty]' => $this->_dirty, '[original]' => $this->_original, '[virtual]' => $this->_virtual, diff --git a/src/ORM/Table.php b/src/ORM/Table.php index dbcfb1afc86..f5f218da3e5 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -1902,7 +1902,7 @@ public function save(EntityInterface $entity, $options = []) '_primary' => true ]); - if ($entity->canBePersisted() === false) { + if ($entity->hasErrors($options['associated']) === true) { return false; }