How update a database table record in Zend?
Asked Answered
J

6

22

I am using select like this and it is fetching record successfully:

$table = new Bugs();
$select = $table->select();
$select->where('bug_status = ?', 'NEW');
$rows = $table->fetchAll($select);

But Now I want to update same record. For example in simple MySQL.

UPDATE TableName Set id='2' WHERE id='1';

How to execute above query in Zend ?

Thanks

Joon answered 4/11, 2010 at 13:43 Comment(0)
J
40
$data = array(
   'field1' => 'value1',
   'field2' => 'value2'
);
$where = $table->getAdapter()->quoteInto('id = ?', $id)

$table = new Table();

$table->update($data, $where);
Jamestown answered 4/11, 2010 at 13:57 Comment(1)
You sir get bonus marks for the $where = $table->getAdapter()->quoteInto('id = ?', $id) shortcut.Galleon
T
12

Since you're already fetching the row you want to change, it seems simplest to just do:

$row->id = 2;
$row->save();
Taal answered 5/11, 2010 at 2:49 Comment(0)
H
10

just in case you wanna increment a column use Zend_Db_Expr eg:

$table->update(array('views' => new Zend_Db_Expr('views + 1')),$where);
Haag answered 4/9, 2012 at 16:4 Comment(0)
U
4

For more than one where statement use the following.

$data = array(
    "field1" => "value1",
    "field2" => "value2"
);
$where['id = ?'] = $id;
$where['status = ?'] = $status;

$table = new Table();

$table->update($data, $where);
Unfledged answered 11/5, 2015 at 15:6 Comment(0)
B
2
public function updateCampaign($id, $name, $value){
    $data = array(
        'name' => $name,
        'value' => $value,
    );
    $this->update($data, 'id = ?', $id );
}
Beck answered 7/9, 2012 at 9:33 Comment(0)
R
1
   $data = array(
    "field1" => "value1",
    "field2" => "value2"
);

$where = "id = " . $id;

$table = new Table();

$table->update($data, $where);
Rebuild answered 4/11, 2010 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.