CakePHP 3 find(),每行都有顺序
CakePHP 3 find() with order foreach each row
伙计,我不知道为什么这不起作用,我正在逐字跟踪 Manuel。
我有一个包含订单列的 ProductStyles Table。假设我有 5 行。我删除了数字 3。我想找到与产品 ID 匹配的所有行,并遍历每一行以递增方式替换订单 #。
Productstyle Table
id - product_id - order
我的第一步是按顺序获取所有与产品 ID 匹配的行,这样我就可以执行 foreach,但我一直收到 SQL 错误。老实说,除了基本的 select.
之外,我对 SQL 不太了解
$query = $this->ProductStyles->find()
->where(['product_id'=> 1 ])
->order(['order' => 'ASC'])
;
//debug($query);
//debug( $query->all() ); //this throws an error
错误信息:
错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有误;查看与您的 MySQL 服务器版本相对应的手册,了解在第 1
行 'order ASC' 附近使用的正确语法
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++;
}
order
是保留字,不允许以不带引号(在本例中为非反引号)的方式使用,需要以 `order`
的形式使用
见
- https://dev.mysql.com/doc/refman/5.7/en/keywords.html
- https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
我建议更改列名,这是最简单且不影响性能的修复方法。另一种选择是启用自动标识符引用,但这会影响性能,并且不能应用于所有地方,参见
Cookbook > Database Access & ORM > Database Basics > Identifier Quoting
order
是SQL中的保留关键字,所以,如果要用作列名,需要使用引号,像这样:
SELECT * FROM table ORDER BY `order` ASC
如您在错误中所见,未被引用。
也许您必须使用 $this->Model->query()
并手动编写查询。
另一个解决方案是将 "order" 列更改为另一个名称。
希望对您有所帮助。
伙计,我不知道为什么这不起作用,我正在逐字跟踪 Manuel。
我有一个包含订单列的 ProductStyles Table。假设我有 5 行。我删除了数字 3。我想找到与产品 ID 匹配的所有行,并遍历每一行以递增方式替换订单 #。
Productstyle Table
id - product_id - order
我的第一步是按顺序获取所有与产品 ID 匹配的行,这样我就可以执行 foreach,但我一直收到 SQL 错误。老实说,除了基本的 select.
之外,我对 SQL 不太了解$query = $this->ProductStyles->find()
->where(['product_id'=> 1 ])
->order(['order' => 'ASC'])
;
//debug($query);
//debug( $query->all() ); //this throws an error
错误信息:
错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有误;查看与您的 MySQL 服务器版本相对应的手册,了解在第 1
行 'order ASC' 附近使用的正确语法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++;
}
order
是保留字,不允许以不带引号(在本例中为非反引号)的方式使用,需要以 `order`
的形式使用
见
- https://dev.mysql.com/doc/refman/5.7/en/keywords.html
- https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
我建议更改列名,这是最简单且不影响性能的修复方法。另一种选择是启用自动标识符引用,但这会影响性能,并且不能应用于所有地方,参见
Cookbook > Database Access & ORM > Database Basics > Identifier Quoting
order
是SQL中的保留关键字,所以,如果要用作列名,需要使用引号,像这样:
SELECT * FROM table ORDER BY `order` ASC
如您在错误中所见,未被引用。
也许您必须使用 $this->Model->query()
并手动编写查询。
另一个解决方案是将 "order" 列更改为另一个名称。
希望对您有所帮助。