0
votes

I have the config 'prefix' => 'hq_', and I created a table : hq_products(pd_id, pd_price, pd_name,pd_date).

In controller, I want to delete a product. I used:

$productId = (int) $this->params['url']['id'];
$this->Product->deleteAll(array('Product.pd_id' => $productId));

and received the error :

Warning (512): SQL Error: 1054: Unknown column 'Product.id' in 'field list' [CORE\cake\libs\model\datasources\dbo_source.php, line 684]

Query:

SELECT `Product`.`id` FROM `hq_products` AS `Product` WHERE `Product`.`pd_id` = 1 

I also used : $this->Product->delete($productId); same error.

Please help me.

1
Show us your table structure. Anyway, are you sure you have a id field in your table?Aurelio De Rosa
Your error message says Product.id for the unknown field, but you're using Product.pd_id in the queries?Marc B
Your query is SELECT, but it should be DELETE. Are you sure you pasted the correct query?JJJ

1 Answers

2
votes

In your Product model, you need to configure the $primaryKey model attribute:

class Product extends AppModel {
    var $primaryKey = 'pd_id';
}

CakePHP expects the primary key in your hq_products table to be id, but you have named it pd_id. You need to inform CakePHP whenever you deviate from the conventions.