How update a database table record in Zend?

2019-04-18 03:10发布

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

6条回答
不美不萌又怎样
2楼-- · 2019-04-18 03:20

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);
查看更多
手持菜刀,她持情操
3楼-- · 2019-04-18 03:33
   $data = array(
    "field1" => "value1",
    "field2" => "value2"
);

$where = "id = " . $id;

$table = new Table();

$table->update($data, $where);
查看更多
Anthone
4楼-- · 2019-04-18 03:36

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

$row->id = 2;
$row->save();
查看更多
Bombasti
5楼-- · 2019-04-18 03:38
$data = array(
   'field1' => 'value1',
   'field2' => 'value2'
);
$where = $table->getAdapter()->quoteInto('id = ?', $id)

$table = new Table();

$table->update($data, $where);
查看更多
Anthone
6楼-- · 2019-04-18 03:38

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

$table->update(array('views' => new Zend_Db_Expr('views + 1')),$where);
查看更多
小情绪 Triste *
7楼-- · 2019-04-18 03:39
public function updateCampaign($id, $name, $value){
    $data = array(
        'name' => $name,
        'value' => $value,
    );
    $this->update($data, 'id = ?', $id );
}
查看更多
登录 后发表回答