我在更新cake php中的多个字段时遇到问题。
这是$product
array(
'sku' => '45',
'name' => 'fefefef22',
'short_description' => '99',
'long_description' => '33',
'price' => '444',
'special_price' => '444',
'stock' => '444',
'brand' => '4',
'is_promo' => '0'
)
my controller
public function update($sku)
{
$this->autoRender = false;
$this->layout = false;
if($this->request->data) {
$this->Product->sku = $sku;
$product = $this->request->data['Product'];
$this->Product->save($product);
}
}
错误消息
1062键"PRIMARY"的重复条目"45";
我尝试了很多其他的动作,但还是传达了同样的信息。
thx
更改此项:
$this->Product->sku = $sku;
对此:
$this->Product->id = $sku;
->id
部分没有设置名为"id"的字段,而是设置主键。因此,在您的情况下,将->id
设置为$sku
实际上是告诉它,您希望字段"sku"的值为该值。
当您使用->sku
时,模型不知道您要设置什么。
->id
指的是模型中的$id
,如CakePHP:的Model.php
中的代码所示
/**
* Value of the primary key ID of the record that this model is
* currently pointing to.
* Automatically set after database insertions.
*
* @var mixed
*/
public $id = false;
这一切都是假设你按照书中的例子正确地在模型中设置了primaryKey:
public $primaryKey = 'sku';