CakePHP OR条件问题

时间:2016-03-04 00:02:50

标签: cakephp

我有2个型号:MandrillEmail(电子邮件日志)和客户端。我想找到一个客户列表:

  • 尚未收到特定的电子邮件
  • 或者已经发送了一封特定的电子邮件(email_content_id = 5),但最后一封邮件是在180天前发送的。

最终,我想将此设置为每180天发送一封电子邮件。我可以轻松完成第一项任务(检查他们是否没有收到电子邮件)。代码如下(这很好用):

$options['joins'][] = 
    array('table' => 'mandrill_emails',
    'alias' => 'MandrillEmail',
    'type' => 'LEFT',
    'conditions' => array(
        'MandrillEmail.client_id = Client.id',
        "MandrillEmail.email_content_id = 5"
    )
);

$options['conditions'][] = array(
    'MandrillEmail.id is NULL'
);

但是,我不确定如何修改查询以检查上面的第二个语句。这就是我所在的地方:

 // check to see if the email HAS NOT been sent, or if it has, that it has been greater than 180 days
 $options['conditions'][] = array(
    'OR' => array(
        'MandrillEmail.id is NULL',
        array(
             "MandrillEmail.status" => 'SENT',  
             "MandrillEmail.created <" => $today_minus_180days
        )
    )
);

另外要记住的是,此电子邮件应每180天发送一次,因此OR语句检查应仅检查最后一个条目。

1 个答案:

答案 0 :(得分:0)

    $options['joins'][] = array(
        'table' => 'mandrill_emails',
        'alias' => 'MandrillEmail',
        'type' => 'LEFT',
        'conditions' => array(
            'MandrillEmail.client_id = Client.id',
            'MandrillEmail.email_content_id' => 5,
            'MandrillEmail.status' => 'SENT',  
            'MandrillEmail.created <' => $today_minus_180days
        )
    );

    $this->Client->virtualFields['have_that_email'] = 'COUNT(MandrillEmail.id)';

如果have_that_email大于0,则电子邮件是从过去180天发送的