• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Entity with model, return empty Object

#1
Question 
Hello there,
Hope you are safe and healthy.
PROBLEM:
PHP Code:
$user $userModel->where('user_email'$email)->first();
// ^ this is null 

Controller Code:
PHP Code:
  public function doLogin()
    {
        if ($this->request->isAJAX())
        {

            $validationRules = array(
                'user_email' => [
                    'rules' => 'required|valid_email|min_length[6]|max_length[150]',
                    'label' => 'email address',
                ],
                'user_password' => [
                    'rules' => 'required|alpha_numeric_punct|min_length[8]|max_length[20]',
                    'label' => 'password',
                ],
            );

            if (!$this->validate($validationRules))
            {
                return $this->commonLib->sendJsonResponse(
                    400,
                    array(
                        "formErrors" => $this->validator->listErrors("revu_error"),
                    )
                );
            }
            else
            {
                $email $this->request->getPost("user_email"FILTER_SANITIZE_EMAIL);
                $password $this->request->getPost("user_password");
                $userModel new  UserModel();
                $user $userModel->where('user_email'$email)->first();
                return $this->commonLib->sendJsonResponse(200$user); // custom function to send json response
            }
        }
        else
        {
            return $this->failForbidden();
        }
    


Model Code:
PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
UserModel extends Model
{

    protected $table 'mg_users';
    protected $primaryKey 'user_id';

    protected $returnType 'App\Entities\User';
    protected $useSoftDeletes FALSE;

    protected $allowedFields = [
        'user_full_name',
        'user_email',
        'user_contact',
        'user_country_code',
        'user_email',
        'user_password',
        'user_is_claimer',
        'user_department',
        'user_role',
        'user_join_on',
        'user_status',
    ];

    protected $useTimestamps FALSE;
    protected $createdField 'user_join_on';
    // protected $updatedField  = 'updated_at';
    // protected $deletedField  = 'deleted_at';

    protected $validationRules = [
        'user_full_name' => 'required|alpha_numeric_space|min_length[3]|max_length[100]',
        'email' => 'required|valid_email',
        'user_password' => 'required|min_length[8]',
        'user_role' => 'required',
        'user_status' => 'required'
    ];

    protected $validationMessages = [];
    protected $skipValidation false;

    


User Entity Code:
PHP Code:
<?php namespace App\Entities;

use 
CodeIgniter\Entity;

class 
User extends Entity
{

    protected $user_id;
    protected $user_full_name;
    protected $user_contact;
    protected $user_country_code;
    protected $user_email;
    protected $user_password;
    protected $user_otp;
    protected $user_otp_expire;
    protected $user_using_facebook;
    protected $user_facebook_id;
    protected $user_facebook_token;
    protected $user_facebook_profile;
    protected $user_using_google;
    protected $user_google_id;
    protected $user_google_token;
    protected $user_is_claimer;
    protected $user_department;
    protected $user_role;
    protected $user_join_on;
    protected $user_status;

    public function verify_pass(string $user_password)
    {
        return password_verify($user_password$this->user_password);
    }


If I Update the return type of model to OBJECT everything works fine but if i keep the return type as entity, it returns empty object.
What is it that I am doing wrong?
Reply

#2
If no records are found, you will get null.

Replace where statement with ID to test, e.g.

PHP Code:
$user $userModel->where('user_id'1)->first(); 

Otherwise nothing obvious which I can help with.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.