Reputation: 61
I am trying to get multiple of two columns as value but in cakephp 3.0 it given a error
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',
]);
Upvotes: 1
Views: 621
Reputation: 60463
That's not how you define calculated columns, please refer to the docs
Cookbook > Database Access & ORM > Query Builder > Selecting Data
Cookbook > Database Access & ORM > Query Builder > Raw Expressions
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(/* ... */)
// ...
Upvotes: 3