cakephp 3.0获取两个coulmns中的值作为一个 [英] cakephp 3.0 get values in two coulmns as one

查看:171
本文介绍了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(/* ... */)
    // ...

这篇关于cakephp 3.0获取两个coulmns中的值作为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆