CakePHP更新问题(重复条目)

CakePHP更新问题(重复条目),php,cakephp,Php,Cakephp,我在更新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'

我在更新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'
)

错误消息

1062键“主”的重复条目“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:


您是否已将表条目检查到数据库中?出现此错误的原因是您的表中已经存在45尝试从$this->request->dataI中删除'sku',它仍然会尝试插入新条目而不是更新行,我建议您执行
echo$this->Form->input('sku')也在您的视图中。如果Cake知道
sku
是您的主键,那么该字段将被隐藏,并且已经包含在您的
$request->data
@iso27002中-这样做当然没有问题,但我们不知道他为什么在URL中设置
$sku
,的初衷,所以很难提出这一点,也与他所问的问题无关。
/**
 * 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;
public $primaryKey = 'sku';