简体   繁体   中英

Zend Framework DB update

Is there something similar to save for the update task?

If I want to save a new record I just do it:

$data_from_post = $_POST;
$newUser = $usersDb->fetchNew();
$newUser->setFromArray($data_from_post);
$newUser->save();

Is there something for the update task?

Thanks and best regard´s.

$where = $usersDb->getAdapter()->quoteInto('id = ?', data_from_post['id']);
$usersDb->update($data_from_post, $where);

Assuming you have a id field in your post array. Basically update takes two params. The update array and a where clause.

see Updating Rows in a Table in here

You want to use $newUser->save() but your $newUser needs to be propagated from a $usersDb->find($_POST['id']); instead of a fetchNew(). And, of course, you'll need to update the $newUser with the new values from $_POST after you've instantiated. The save() method checks for modified fields and routes to update() instead of insert();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM