2013-05-11 77 views
1

我想如下做一個updateAll:如何updateAll使用CakePHP

// initialize the array 
$array = array(
    "Land Rover" => array("LAND ROVER") 
); 

// loop both arrays 
foreach($array as $new => $aOld) { 
    foreach($aOld as $old) { 
     $this->updateAll(array('make = $new'), array('make = $old')); 
    } 
} 

,但我得到的錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Rover WHERE `make` = 'LAND ROVER'' at line 1 

我是後是:

UPDATE items SET make = 'Land Rover' WHERE make = 'LAND ROVER'; 

請不要給我關於大小寫轉換的答案,因爲我有其他更多的數組元素。

如何避免該錯誤?其實,我如何轉儲完整的SQL?我在Console中運行此代碼。

非常感謝。

編輯:我剛剛得到的SQL調試:

UPDATE `items` AS `Item` SET `Item`.`id` = make = "Land Rover" WHERE `make` = '\"LAND ROVER\"' 

一個明顯的問題,但它是如何到達那裏?

+0

指這將是挑釁幫助你http://book.cakephp.org/2.0/en/models/saving-your-data.html – liyakat 2013-05-11 11:16:37

+0

@liyakat我已經有了。通過調整所示示例,我得到了錯誤。 – khany 2013-05-11 11:46:58

回答

2

值和條件應作爲關聯傳遞陣列;

$this->updateAll(
    array(
     'Item.make' => Sanitize::escape($new) 
    ), 
    array(
     'Item.make' => $old 
    ) 
); 

注意; 我不是在我的電腦後面,不知道是否Sanitize::escape()已經報價的值,否則;

array(
    'Item.make' => "'" . Sanitize::escape($new) . "'" 
), 
0

您需要引用在$fields值帕拉姆自己喜歡API說:

$this->updateAll(array("make = \"$new\""), array('make = $old')); 

更可讀的版本是:

$this->updateAll(array('make' => "\"$new\""), array('make' => $old)); 
+0

第一個產生沒有錯誤,但沒有做任何事情。第二個在make字段中放置'$ new'。 – khany 2013-05-11 11:59:04

+0

檢查更新後的語句。 – ADmad 2013-05-11 13:40:06

0
$this->modelname->updateAll(array('make'=>"'$new'"), array('modelname.make'=>$old)) 

i hope its working