Search code examples
sql-updateentitycakephp-3.0detect

Update a row(entity) in a table only if a change detected(automatically)


Sorry for my bad english :(

I'm using the CAKEPHP 3 framework and i want to update a row(entity) in a table only if a change detected(automatically).

Eg.:

$id = 1;
// debug($this->request->data)
$this->request->data = ['id' => 1, 'name' => "My Name", 'content' => "My Content"];
// Inside the ArticlesController
$article = $this->Articles->get($id);
// debug($article)
object(Cake\ORM\Entity) {
    'id' => (int) 1,
    'name' => "My Name",
    'content' => "My Content",
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Articles'
}

There is no difference between the request data and the original data so i have to reject the update query and return a warning to the user "No changes detected !"

What i have to do ?

Thanks


Solution

  • Each entity has a field 'dirty' which shows if the entity has been updated.

    You could use something like :

    if($article->dirty()) {
        // send a flash message
    }
    

    Check the book here : http://book.cakephp.org/3.0/en/orm/entities.html#checking-if-an-entity-has-been-modified