web-dev-qa-db-ja.com

Zendでデータベーステーブルレコードを更新するにはどうすればよいですか?

私はこのようなselectを使用しており、レコードを正常にフェッチしています:

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

しかし、今は同じレコードを更新したいと思います。たとえば、単純なMySQLの場合です。

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

上記のクエリをZendで実行する方法は?

ありがとう

21
Awan
$data = array(
   'field1' => 'value1',
   'field2' => 'value2'
);
$where = $table->getAdapter()->quoteInto('id = ?', $id)

$table = new Table();

$table->update($data, $where);
40
Alex Pliutau

変更する行をすでにフェッチしているので、次のようにするのが最も簡単なようです。

$row->id = 2;
$row->save();
11
Tim Lytle

カラムをインクリメントしたい場合に備えて、Zend_Db_Exprを使用してください。例:

$table->update(array('views' => new Zend_Db_Expr('views + 1')),$where);
9
Srinivas R

複数のwhereステートメントの場合は、以下を使用します。

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

$table = new Table();

$table->update($data, $where);
4
Waqas Bukhary
public function updateCampaign($id, $name, $value){
    $data = array(
        'name' => $name,
        'value' => $value,
    );
    $this->update($data, 'id = ?', $id );
}
2
zendframeworks
   $data = array(
    "field1" => "value1",
    "field2" => "value2"
);

$where = "id = " . $id;

$table = new Table();

$table->update($data, $where);
1
mitch