当我用$this->query
执行一些查询时,CakePHP返回这个:
:
<?php
App::uses('AppModel', 'Model');
class Authorization extends AppModel {
public $displayField = 'name';
public $useTable = 'auth';
public $actsAs = array();
SQL in Model:
public function getNotUsed($initialDate, $finalDate)
{
$sql = "SELECT auth_num ,
auth_code ,
amount AS verisign_amount ,
print_amount ,
card_name ,
a.table_num AS table_num ,
a.add_date AS date ,
'Tb:'|| table_num || ' - ' || auth_code || ' - $' || print_amount AS code_print_amount
FROM auth a
WHERE trxtype = 'S'
AND gift_card = 'N'
AND used = 'N'
AND a.add_date BETWEEN '$initialDate' AND '$finalDate'
AND pnref NOT IN
(SELECT origid
FROM auth a
WHERE add_date BETWEEN '$initialDate' AND '$finalDate'
AND trxtype = 'V')
ORDER BY add_date";
return $this->query($sql);
}
Array
(
[0] => Array
(
[0] => Array
(
[auth_num] => 536825
[auth_code] => 0000000
[verisign_amount] => 0.50
[print_amount] => 0.50
[card_name] => TEST
[table_num] => 37
[date] => 02/20/2013 14:56:35
[code_print_amount] => Tb:37 - 198198 - $0.50
)
)
)
我想要这个(当搜索超过1行):
Array
(
[0] => Array
(
['Authorization'] => Array
(
[auth_num] => 536825
[auth_code] => 0000000
[verisign_amount] => 0.50
[print_amount] => 0.50
[card_name] => TEST
[table_num] => 37
[date] => 02/20/2013 14:56:35
[code_print_amount] => Tb:37 - 198198 - $0.50
)
)
)
或者this (when search more 1 row - limit 1):
Array
(
['Authorization'] => Array
(
[auth_num] => 536825
[auth_code] => 0000000
[verisign_amount] => 0.50
[print_amount] => 0.50
[card_name] => TEST
[table_num] => 37
[date] => 02/20/2013 14:56:35
[code_print_amount] => Tb:37 - 198198 - $0.50
)
)
我如何执行这个查询并获得第一个数组键(所有字段)。在本例中,第二个数组键[0]。或者只是一个[0] => ['Authorization'] => array()
。
- CakePHP 2.3.0
Apache 2.2Postgres 8.4
仅在确实有必要时使用RAW SQL
首先,尽量避免使用RAW SQL查询,除非真的没有其他选择。另外,在使用RAW SQL时,请记住您必须自己清理查询,以防止SQL注入。由于您正在处理支付信息,请再次检查您是否正在转义$initialDate
和$finalDate
,因为我在您的代码中没有看到任何安全措施。
格式化结果
为了得到你想要的结果,你必须为你的字段使用特殊格式的别名;
SELECT
myfield AS "Mymodel__somefield",
myotherfield AS "Mymodel__someotherfield"
FROM .....
注意在Mymodel
和fieldname
之间应该有两个下划线
应该返回;
array (
0 => array(
'Mymodel' => array (
'somefield' => 'foo',
'otherfield' => 'bar',
)
)
)
因此,接受@thaJeztah的建议,您的查询需要更改为以下内容:
$sql = "SELECT `Authentication`.`auth_num`,
`Authentication`.`auth_code`,
`Authentication`.`amount` AS `Authentication`.`verisign_amount`,
`Authentication`.`print_amount`,
`Authentication`.`card_name`,
`Authentication`.`table_num`,
`Authentication`.`add_date` AS `Authentication`.`date`,
'Tb:'|| `Authentication`.`table_num` || ' - ' || `Authentication`.`auth_code` || ' - $' || `Authentication`.`print_amount` AS `Authentication`.`code_print_amount`
FROM `auth` as `Authentication`
WHERE `Authentication`.`trxtype` = 'S'
AND `Authentication`.`gift_card` = 'N'
AND `Authentication`.`used` = 'N'
AND `Authentication`.`add_date` BETWEEN '?' AND '?'
AND `Authentication`.`pnref` NOT IN
(SELECT o`Authentication`.`rigid`
FROM `auth` as `Authentication`
WHERE `Authentication`.`add_date` BETWEEN '?' AND '?'
AND `Authentication`.`trxtype` = 'V')
ORDER BY `Authentication`.`add_date`";
我实际上花了一点机会来猜测哪些表的东西来自。给字段命名空间非常重要,这样就不会让数据库猜测字段来自哪里。
也是为了回应@theJeztah的回答,尽量避免自己编写SQL。如果您有子查询,那么尝试将它们分解为多个$this->model->find(...)
。它没有那么快,但可能更安全。
您的代码可能是:
$this->query('select * etc');