简体   繁体   English

CakePHP同时检查两个查找条件

[英]CakePHP check two find conditions at the same time

I have the following find code: 我有以下查找代码:

if($this->User->find('first', array(
                        'conditions' => array(
                            'User.username' => $this->data['User']['username'],
                            'User.email' => $this->data['User']['username'])
                    )))

The idea is that it will find if a user exists with an email or username that matches what was sent in the username input field. 其想法是,它将查找用户是否存在与用户名输入字段中发送的电子邮件或用户名相匹配的电子邮件或用户名。

The problem is that in fact it will try and match BOTH rather than one or the other which is what I really want. 问题是实际上它会尝试同时匹配两者,而不是我真正想要的一个。 How do I do this though? 但是我该怎么做呢?

I tried with the following but still no success: 我尝试了以下方法,但仍然没有成功:

if($this->User->find('first', array(
                        'conditions' => array('OR' => array(
                            array('User.username' => $this->data['User']['username']),
                            array('User.email' => $this->data['User']['username'])))))

Thanks 谢谢

You were close, it should go like this 你很近,它应该像这样

$opts = array(
   'conditions' => array(
       'or' => array(
          'User.username' => $this->data['User']['username'],
          'User.email' => $this->data['User']['username']
        )
    )
)
$this->User->find('first', $opts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM