【问题标题】:cakePHP :Find all query conditions questionscakePHP :查找所有查询条件问题
【发布时间】:2013-07-09 17:59:07
【问题描述】:

我正在尝试格式化查找条件以生成一组数据,以便稍后通过 PhPExcel 打印到 Excel 视图。预期的功能是,如果 start_date 和 end_date 字段与条目匹配,则该条目将包含在数组中。如果字段不匹配,则将其排除。

    $my_data = $this-> RecordsMaster-> find("all", array(
        'conditions' => array(
            "RecordsMaster.start_date" == "RecordsMaster end_date"
        )
    ));

问题在于上面的语句实际上并没有做任何事情。 mySQL 表中开始日期和结束日期不同的行包含在数据转储到 Excel 中。

【问题讨论】:

  • 问题出在哪里?你收到错误信息? :) 请完成您的问题
  • 抱歉,今天办公室的咖啡用完了。 :P
  • 我很惊讶您没有收到与使用 == 作为数组键值终止符有关的错误。你可能想看看这个:bakery.cakephp.org/articles/lucaswxp/2011/02/11/…

标签: cakephp


【解决方案1】:

问题中的条件意义不大

问题中的条件等价于:

$my_data = $this-> RecordsMaster-> find("all", array(
    'conditions' => array(
        false
    )
));

这将不返回任何记录,因为它将生成以下 sql:

WHERE 0;

不要使用 key => value 来匹配字段

更正问题中代码中的拼写错误仍然不会生成所需的 sql - 它会生成:

WHERE `RecordsMaster`.`start_date` = "RecordsMaster end_date"

即其中start_date 是那个文字字符串。

预期的功能是,如果 start_date 和 end_date 字段与条目匹配

要生成WHERE field = otherfield,您可以指定为字符串:

$my_data = $this-> RecordsMaster-> find("all", array(
    'conditions' => array(
        "RecordsMaster.start_date = RecordsMaster.end_date"
    )
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多