使用Cakephp找不到正确的别名

时间:2012-09-01 15:07:08

标签: mysql cakephp alias

我是CakePHP的新手,面临SQL Alias的问题。 我想制作一个Excel文件来从MySQL数据库生成报告。

/**
     * Generate transaction report
     *
     * @param none
     *
     * @return void
     */
    public function generateReport()
    {

         //Getting data from the database to generate the report in Excel file
        $data = $this->AchTransaction->find('all', array(
            //'conditions' => $searchCondition,
            'fields' => array(
                    "payee_name AS PAYEENAME",
                    'reference AS REFERENCE',
                    'reference2 AS REFERENCE2',
                    'currency AS CURRENCY',
                    'amount AS PAYOUTAMOUNT',
                    'bank_name AS BANKNAME',
                    'bank_branch_name AS BANKBRANCHNAME',
                    'bank_address AS BANKADDRESS',
                    'bank_country AS BANKCOUNTRY',
                    'account_name AS ACCOUNTNAME',
                    'account_class AS  ACCOUNTCLASS',
                    'account_type AS ACCOUNTTYPE',
                    'bank_routing_number AS BANKROUTING',
                    'account_number AS ACCOUNT',
                    'beneficiary_address AS BENEFICIARYADDRESS',
                    'beneficiary_city AS BENEFICIARYCITY',
                    'beneficiary_state AS BENEFICIARYSTATE',
                    'beneficiary_postcode AS BENEFICIARYZIPCODE',
                    'beneficiary_country AS BENEFICIARYCOUNTRY',
                    'note as NOTE'
                    ),
            'order' => array('AchTransaction.id DESC')
                ));



        // Generating Excel file
        $this->Common->generateReport($data);
    }

我能够生成Excel但在别名中只有一个问题,正如您所见

"payee_name AS PAYEENAME",

这里payee_name是MySQL表中列或字段的名称,我希望它为PAYEE NAME,但是当我写道时:

"payee_name AS PAYEE NAME", 

我收到了这个MySQL错误:

Database Error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'payee_name AS PAYEE NAME' in 'field list'

SQL Query: SELECT `payee_name AS PAYEE NAME`, `reference` AS `REFERENCE`, `reference2` AS `REFERENCE2`, `currency` AS `CURRENCY`, `amount` AS `PAYOUTAMOUNT`, `bank_name` AS `BANKNAME`, `bank_branch_name` AS `BANKBRANCHNAME`, `bank_address` AS `BANKADDRESS`, `bank_country` AS `BANKCOUNTRY`, `account_name` AS `ACCOUNTNAME`, `account_class` AS `ACCOUNTCLASS`, `account_type` AS `ACCOUNTTYPE`, `bank_routing_number` AS `BANKROUTING`, `account_number` AS `ACCOUNT`, `beneficiary_address` AS `BENEFICIARYADDRESS`, `beneficiary_city` AS `BENEFICIARYCITY`, `beneficiary_state` AS `BENEFICIARYSTATE`, `beneficiary_postcode` AS `BENEFICIARYZIPCODE`, `beneficiary_country` AS `BENEFICIARYCOUNTRY`, `note` AS `NOTE` FROM `aramor_payments`.`ach_transactions` AS `AchTransaction` WHERE 1 = 1 ORDER BY `AchTransaction`.`id` DESC

Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
Stack Trace

    CORE\Cake\Model\Datasource\DboSource.php line 461 → PDOStatement->execute(array)
    CORE\Cake\Model\Datasource\DboSource.php line 427 → DboSource->_execute(string, array)
    CORE\Cake\Model\Datasource\DboSource.php line 671 → DboSource->execute(string, array, array)
    CORE\Cake\Model\Datasource\DboSource.php line 1082 → DboSource->fetchAll(string, boolean)
    CORE\Cake\Model\Model.php line 2631 → DboSource->read(AchTransaction, array)
    APP\Controller\AchtransactionsController.php line 353 → Model->find(string, array)
    [internal function] → AchtransactionsController->generateReport()
    CORE\Cake\Controller\Controller.php line 485 → ReflectionMethod->invokeArgs(AchtransactionsController, array)
    CORE\Cake\Routing\Dispatcher.php line 186 → Controller->invokeAction(CakeRequest)
    CORE\Cake\Routing\Dispatcher.php line 161 → Dispatcher->_invoke(AchtransactionsController, CakeRequest, CakeResponse)
    APP\webroot\index.php line 92 → Dispatcher->dispatch(CakeRequest, CakeResponse)

是否可以帮助我将payee_name字段的名称设为PAYEE NAME

1 个答案:

答案 0 :(得分:5)

更改

"payee_name AS PAYEE NAME", 

"`payee_name` AS `PAYEE NAME`"

另一种选择是使用virtualFields

$this->AchTransaction->virtualFields['PAYEE NAME'] = 'payee_name';
array('fields' => array('PAYEE NAME', 'etc'))