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条)

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理