We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PHQL if condition is not returning data

I am trying to get json response in my app, and for that I've created this action in controller class

public function signupAction()
{
    $this->view->disable();

    //Create a response instance
    $response = new \Phalcon\Http\Response();

    $robots = Users::find(array(
        "uname = 'viralj'",
    ));

    $data = array();
    if(!empty($robots)){

        foreach ($robots as $robot) {
            $data[] = array(
                'error' => 'false',
                'userfound' => 'true',
                'id'    => $robot->id,
                'fname' => $robot->fname,
                'lname' => $robot->lname,
                'email' => $robot->email,
                'pass'  => $robot->password,
                'reg'   => $robot->regdatetime
            );
        }

    }else{
        $data[] = array("error" => "true", "userfound" => "false");
    }

    echo json_encode($data);

}

so when I try to access the required page, I get details from my database because I have a user with username of "viralj". But when I try to check with false details, like "abc" username which I don't have in my database, insted of getting this ("error" => "true", "userfound" => "false") in json format, i am getting brackets lke this [] in my webpage. so can anyone tell me how can i figure this out? And also if I want to check that if someone is login with username or email how can i check this in one query? What codes should I write in this with condition of username or email

$robots = Users::find(array(
        "uname = 'viralj'",
    ));

Please let me know. Thank you for your time.



58.4k

To be check user login with email you must to

    $robots = Users::findFirstByEmai($email)

But I am allowing users to use either username or email with password to login in the website. So my question is that how can i perform "SELECT id FROM users WHERE (email = '[email protected]' OR uname = 'UserName') AND password = 'password'" this query in PHQL?

To be check user login with email you must to

  $robots = Users::findFirstByEmai($email)


58.4k

it will not working if password encrypt in database

You can use

    $object = User::findByEmailOrUsername($this->request->getPost('email'));
                    if ($object && $object->getStatus() == $object::STATUS_ACTIVE) {
                        if (!$this->security->checkHash($this->request->getPost('password'), $object->getPasswd())) {
                            $this->flashSession->error(t('Wrong email/password combination!'));
                        }


1.7k
edited Nov '14

Your logic is not good. It should be something like

$robot = Users::findFirst(array(
    "uname = 'viralj' OR email = '[email protected]'"
));

//If there is no record in your database match above condition the $robot value will be false, so the next statement should look like

if($robot){
    //Place your logic here
}
else{
    $data[] = array("error" => "true", "userfound" => "false");
}