Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Getting the value in a column problem

Hi guys,

Im having a hard tym on this already. I wanted to get the usertype base on the email. I have 2 records

usertype    email                           password
1               [email protected]    admin1
2               [email protected]    admin2

here is my code

    public function signinAction(){

        Tag::appendTitle(' | Sign in');

        /**
         * include file forms/LoginForm.php
         * 
         */
        self::$signin_form = new LoginForm;
        $this->view->form = self::$signin_form;

        if(self::$hit->isPost()){

            $this->signinvalidateAction();
        }
    }

    private function signinvalidateAction(){

        self::$email    = self::$hit->getPost('lf_email', ['string', 'striptags']);
        self::$password = self::$hit->getPost('lf_password', ['string', 'striptags', 'trim']);

        switch(self::$signin_form){

            case (self::$signin_form->isValid($_POST)):

                $checkuser = TblUser::findFirstByEmail(self::$email);

                if($checkuser) {
                    if (self::$hash->checkHash(self::$password, $checkuser->password)) {

                        parent::__beginsession(self::$email);

                        $check_usertype = TblUser::findFirst($checkuser);
                        $result_usertype = $check_usertype->user_type_fk;

                        echo $result_usertype;
                        //$this->response->redirect('dashboard');
                    }
                    else{

                        echo $this->flash->error("Invalid email/password");
                    }
                }
            break;

            default:
                    foreach (self::$signin_form->getMessages() as $message){
                        echo $this->flash->error($message);
                    }
            break;
        }
    }

the problem is it shows usertype is 1 even if i logged admin2

thnks guys



6.5k
Accepted
answer

It looks like you use two finds on the same table

               $checkuser = TblUser::findFirstByEmail(self::$email);

                if($checkuser) {
                    if (self::$hash->checkHash(self::$password, $checkuser->password)) {

                        parent::__beginsession(self::$email);

                        $check_usertype = TblUser::findFirst($checkuser);
                        $result_usertype = $check_usertype->user_type_fk;

                        echo $result_usertype;

You should be able to simplify this to

               $checkuser = TblUser::findFirstByEmail(self::$email);

                if($checkuser) {
                    if (self::$hash->checkHash(self::$password, $checkuser->password)) {

                        parent::__beginsession(self::$email);
                        echo $checkuser->user_type_fk;

Feel free to refer to the vokuro app example if you are having issues. It makes a lot of the phalcon concepts 'click' and can make everything a lot easier.

thnks @David Duncan, it worked!! =)