سؤال

i got this error when user name or password is wrong. run successfully while correct username and password. Call to a member function count() on a non-object in ....

my code is

public function executeLogin(sfWebRequest $request)
{
    $this->form = new loginForm();

    if ($request->isMethod('post'))
    {
        $this->form->bind($request->getParameter('login'));
        if ($this->form->isValid())
        {
            $unm=$this->form->getValue('username');
            $pwd=$this->form->getValue('password');
            $q = Doctrine_Query::create()
            ->select('id')
            ->from('login')
            ->andWhere('username = ?', $unm)
            ->andWhere('password = ?', $pwd);
            $q->execute();
            $result = $q->fetchOne();
            if ($result->count() > 0) 
            {            
                $this->getUser()->setAuthenticated(true);
                $this->getUser()->addCredential('user');
                $this->redirect('user/index');
            } 
            else 
            {
                $this->redirect('user/login');
            }   
        }
    }
}
هل كانت مفيدة؟

المحلول

$result value has NULL value. You should check it first. Let's change conditions to the following:

if ($result && $result->count() > 0) {...}

or even

if ($result) {...}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top