2010-11-04 9 views
21

Używam wybierz tak i jest to rekord pobierania pomyślnie:Jak aktualizować rekord tabeli bazy danych w Zend?

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

ale teraz chcę zaktualizować sam rekord. Na przykład w prostym MySQL.

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

Jak wykonać powyższe zapytanie w Zend?

Dzięki

Odpowiedz

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

$table = new Table(); 

$table->update($data, $where); 
+0

To działa. Dzięki – Awan

+4

Otrzymujesz bonusowe znaki dla skrótu '$ where = $ table-> getAdapter() -> quoteInto ('id =?', $ Id)'. –

+1

Tak, jasne, proszę pana. – pltvs

1
$data = array(
    "field1" => "value1", 
    "field2" => "value2" 
); 

$where = "id = " . $id; 

$table = new Table(); 

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

Masz niepoprawną klauzulę where. Zobacz moją odpowiedź. – pltvs

11

Skoro jesteś już ściągam wiersz, który chcesz zmienić, wydaje się najprostsze, aby po prostu zrobić:

$row->id = 2; 
$row->save(); 
9

tylko w przypadku chcesz zwiększyć kolumnę użyj Zend_Db_Expr np .:

$table->update(array('views' => new Zend_Db_Expr('views + 1')),$where); 
2
public function updateCampaign($id, $name, $value){ 
    $data = array(
     'name' => $name, 
     'value' => $value, 
    ); 
    $this->update($data, 'id = ?', $id); 
} 
1

Dla więcej niż jednej instrukcji where użyj poniższego.

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

$table = new Table(); 

$table->update($data, $where); 
Powiązane problemy