私は CakePHP が初めてで、SQL エイリアスの問題に直面しています。MySQL データベースからレポートを生成するための Excel ファイルを作成したいと考えています。
/**
* 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 を生成することはできますが、エイリアスに問題が 1 つだけあります。ご覧のとおり
"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
か?