当我使用$this->query
执行某些查询时,CakePHP会返回:
模型:
<?php
App::uses('AppModel', 'Model');
class Authorization extends AppModel {
public $displayField = 'name';
public $useTable = 'auth';
public $actsAs = array();
模型中的SQL :
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
)
)
)
或者这个(当搜索更多1行 - 限制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()
。
我正在使用:
答案 0 :(得分:4)
首先,尝试避免使用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',
)
)
)
答案 1 :(得分:1)
因此,根据@ 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(...)
。它不是那么快,但它可能更安全。
答案 2 :(得分:0)
您的代码可能是:
$this->query('select * etc');
如果这不起作用,请向我们展示更多代码。