cakephp 3.0获取两个coulmns中的值作为一个
这里我试图获取多个两列作为值,但在cakephp 3.0它给出一个错误
Here I am trying to get multiple of two columns as value but in cakephp 3.0 it given a error
错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请检查与您的MySQL服务器版本对应的手册,以获取在AS(Transactions__amount * PluTransaction FROM 事务
事务
LEF'
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 'AS (Transactions__amount * PluTransaction FROM transactions
Transactions
LEF'
$result = $this->Transaction->find('all', array(
'conditions' => [
'Transactions.house_id' => $houseId]
))->join([
[
'alias' => 'PluTransaction',
'table' => 'plu_transactions',
'type' => 'LEFT',
'conditions' => 'PluTransaction.transaction_id = Transactions.id'
]
])->select(['Transactions.id',
'(Transactions.amount * PluTransaction.item_quantity) AS TOTAL',
]);
这不是如何定义计算列,请参考文档
That's not how you define calculated columns, please refer to the docs
Cookbook > Database Access & ORM > Query Builder > Selecting Data
Cookbook>数据库访问& ORM>查询生成器>原始表达式
Cookbook > Database Access & ORM > Query Builder > Raw Expressions
c> key => value 格式以单独定义别名和表达式。
You must use the key => value
format to define the alias and the expression separately.
$query = $this->Transaction->find('all', [
'conditions' => [
'Transactions.house_id' => $houseId
]
]);
$query
->select([
'Transactions.id',
'TOTAL' => $query->newExpr('Transactions.amount * PluTransaction.item_quantity')
])
->join(/* ... */)
// ...