drt3751 2015-08-27 17:45
浏览 13
已采纳

CakePHP 3找到(),每行都有一个foreach命令

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++;
    }
  • 写回答

2条回答 默认 最新

  • dongtaidai0492 2015-08-27 18:26
    关注

    order is a reservered word, and is not allowed to be used in an unquoted (in this case non-backticked) fashion, it would need to be used in the form of `order`

    See

    I would suggest to change the column name, that's the easiest, and non-performance affecting fix. Another option would be to enable automatic identifier quoting, this will however affect the performance, and cannot be applied everywhere, see

    Cookbook > Database Access & ORM > Database Basics > Identifier Quoting

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?