1

当我使用 a 执行一些查询时$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().


我在用着:

  • CakePHP 2.3.0
  • PHP 5.3.8
  • 阿帕奇 2.2
  • Postgres 8.4

4

3 回答 3

4

仅在确实必要时使用 RAW SQL

首先,尽量避免使用 RAW SQL 查询,除非真的没有其他选择。此外,在使用 RAW SQL 时,请记住,您必须自己清理查询以防止 SQL 注入。由于您正在处理付款信息,请仔细检查您是否正在转义$initialDate$finalDate因为我在您的代码中没有看到任何安全措施。

格式化结果

要以您想要的方式获得结果,您必须以“特殊”格式为您的字段使用别名;

SELECT
    myfield      AS "Mymodel__somefield", 
    myotherfield AS "Mymodel__someotherfield"

FROM .....

注意和之间应该有两个下划线Mymodelfieldname

应该返回;

array (
    0 => array(
        'Mymodel' => array (
            'somefield' => 'foo',
            'otherfield' => 'bar',
        )
    )
)
于 2013-05-06T19:51:27.307 回答
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(...). 它没有那么快,但它可能更安全。

于 2013-05-06T21:17:42.147 回答
0

您的代码可能是:

$this->query('select * etc');

如果这不起作用,请向我们展示更多代码。

于 2013-05-06T17:48:03.633 回答