在CakePHP 3中的MySQL存储函数的ORDER BY
作者:互联网
我正在使用cakePHP 3.我在mysql数据库中有一个用户定义的函数(UDF或Routine).该函数接受一个参数并返回一个整数值.我必须在MySQL order子句中订购返回的值.
我知道mysql查询使用该功能.即,
SELECT customer_id FROM table_name ORDER BY routine_name(param1); //param1 is 'customer_id' which I have written after SELECT
但我不知道如何在cakePHP 3中构建此查询.如果有人知道解决方案,答案将是欣赏.
这是我的cakePHP 3代码.
$purchasesTable = TableRegistry::get("Purchases");
$query = $purchasesTable->find();
$sf_val = $query->func()->routine_name(['Purchases.customer_id' => 'literal']);
$query->select();
$query->order(
// Routine/Function call should be here as per MySQL query.
// So, I think here I have to do something.
);
解决方法:
我建议你仔细看看(API)文档,这里都提到了.您可以将表达式对象传递给Query :: order(),如果需要指定方向,还有Query :: orderAsc()和Query :: orderDesc().
所以
$expression = $query->func()->routine_name(['Purchases.customer_id' => 'literal']);
$query->order($expression);
要么
$query->orderAsc($expression);
要么
$query->orderDesc($expression);
也可以看看
> Cookbook > Database Access & ORM > Query Builder > Selecting Data
> API > \Cake\Database\Query::order()
> API > \Cake\Database\Query::orderAsc()
> API > \Cake\Database\Query::orderDesc()
标签:php,cakephp,cakephp-3-0 来源: https://codeday.me/bug/20190829/1761057.html