Man, I don’t know why this isn’t working, i’m following the Manuel verbatim.
I have a ProductStyles Table that has an order column. Suppose i have 5 rows. And I delete number 3. I want to find all rows matching the product id and iterate through each one of them replacing the order # incrementially.
Productstyle Table
id - product_id - order
My first step is to just fetch all the rows matching product ID in order so i can do a foreach but I keep get SQL errors. Honestly I don’t know SQL that well other than the basic select.
$query = $this->ProductStyles->find()
->where(['product_id'=> 1 ])
->order(['order' => 'ASC'])
;
//debug($query);
//debug( $query->all() ); //this throws an error
Error Message:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'order ASC' at line 1
SQL:
//SELECT ProductStyles.id AS `ProductStyles__id`, //ProductStyles.product_id AS
//`ProductStyles__product_id`, ProductStyles.style_id AS //`ProductStyles__style_id`,
//ProductStyles.order AS `ProductStyles__order` FROM product_styles ProductStyles
//WHERE product_id = :c0 ORDER BY order ASC
//die;
$i = 1;
foreach ($query as $row) {
//debug($row); die;
$ps= $this->ProductStyles->get($row->id);
//debug($ps);die;
$ps->order = $i;
$this->ProductStyles->patchEntity($ps,['order'=> $i]);
$this->ProductStyles->save($ps);
$i++;
}