Saturday, December 3, 2011

Edit an object without creating it

Hi everybody,
is there a way, given an id, to invoke Model->save() (or anything
else) to update the relative record if and only if the id is valid?
Imagine I want to edit a Post and change its body; if I erroneously
call the edit action passing an id which does not exist,
Model->save($data) will create a new Post with the wrong id.

The following is the implementation of the edit action (very similar
to the add one):

// controller
public edit() {
...
$data = validateInput(...);
if ($this->Post->save($data))
// ok
else
// something went wrong
...
}

What is the right way to avoid this? Should I have to create a helper
function (like the one below) which validates the id and then update
the record and then use save inside the add action, and update inside
the edit one?

// model
public update($data) {
if (!$this->read(null, $data['id']))
return;
$this->save($data);
}


Regards,
Matteo

--
http://www.matteolandi.net/

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate