Reputation: 957
I having trouble updating multiple fields in cake php.
this is $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);
}
}
The error message
1062 Duplicate entry '45' for key 'PRIMARY';
i have tryed lots of other action but still the same message.
thx
Upvotes: 2
Views: 2083
Reputation: 29121
Change this:
$this->Product->sku = $sku;
To this:
$this->Product->id = $sku;
The ->id
part is not setting a field named "id" - it's setting the primary key. So in your case, setting the ->id
to $sku
, is actually telling it that you want the value of the field "sku" to be that value.
The model has no idea what you're trying to set when you use ->sku
.
->id
is referring to $id
in the model as seen in the code in Model.php
of CakePHP:
/**
* 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;
This is all assuming that you correctly set the primaryKey in the model per the book's example:
public $primaryKey = 'sku';
Upvotes: 1