Skip to content
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
7 changes: 7 additions & 0 deletions en/development/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ be thrown from a number of CakePHP core components:

A model's behavior could not be found.

.. php:exception:: PersistenceFailedException

An entity couldn't be saved/deleted while using :php:meth:`Cake\\ORM\\Table::saveOrFail()` or
:php:meth:`Cake\\ORM\\Table::deleteOrFail()`.

.. versionadded:: 3.4.1 PersistenceFailedException has been added.

.. php:namespace:: Cake\Datasource\Exception

.. php:exception:: RecordNotFoundException
Expand Down
23 changes: 23 additions & 0 deletions en/orm/deleting-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,26 @@ A bulk-delete will be considered successful if 1 or more rows are deleted.

deleteAll will *not* trigger beforeDelete/afterDelete events. If you need those
first load a collection of records and delete them.

Strict Deletes
--------------

.. php:method:: deleteOrFail($entity, $options = [])


Using this method will throw an :php:exc:`Cake\\ORM\\Exception\\PersistenceFailedException`
if the entity is new, has no primary key value, application rules checks failed
or the delete was aborted by a callback.

If you want to track down the entity that failed to save, you can use the
:php:meth:`Cake\\ORM\Exception\\PersistenceFailedException::getEntity()` method::

try {
$table->deleteOrFail($entity);
} catch (\Cake\ORM\Exception\PersistenceFailedException $e) {
echo $e->getEntity();
}

As this internally perfoms a :php:meth:`Cake\\ORM\\Table::delete()` call, all corresponding delete events will be triggered.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/perfoms/performs

Copy link
Member

Choose a reason for hiding this comment

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

I'll fix that post-merge.


.. versionadded:: 3.4.1
30 changes: 30 additions & 0 deletions en/orm/saving-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,36 @@ receiving from the end user is the correct type. Failing to correctly handle
complex data could result in malicious users being able to store data they
would not normally be able to.

Strict Saving
=============

.. php:method:: saveOrFail($entity, $options = [])


Using this method will throw an :php:exc:`Cake\\ORM\\Exception\\PersistenceFailedException`
if the application rules checks failed, the entity contains errors or the save was aborted by a callback.
Using this can be helpful when you performing complex database operations without human monitoring,
for example, inside a Shell task.
Copy link
Member

@dereuromark dereuromark Feb 16, 2017

Choose a reason for hiding this comment

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

Should we add:

Do not use this for validation inside controller actions and where feedback to the user has to be given in forms.

etc?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, good point!

Copy link
Member

Choose a reason for hiding this comment

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

One could use it there if they catch the exception.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah but not without catching it then :)

Copy link
Member

Choose a reason for hiding this comment

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

'Do not use' is pretty strongly worded when it can be used if you catch the resulting exceptions.


.. warning::

Do not use this method when the save was triggered by a user, for example,
within a controller action or if you want to give a user feedback within a form.

If you want to track down the entity that failed to save, you can use the
:php:meth:`Cake\\ORM\Exception\\PersistenceFailedException::getEntity()` method::

try {
$table->saveOrFail($entity);
} catch (\Cake\ORM\Exception\PersistenceFailedException $e) {
echo $e->getEntity();
}

As this internally perfoms a :php:meth:`Cake\\ORM\\Table::save()` call, all corresponding save events
will be triggered.

.. versionadded:: 3.4.1

Saving Multiple Entities
========================

Expand Down