• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Empty entity attributes

#1
Question 
Faced a problem, I can not get data from the entity. For example,
Code:
$user_entity->username
always returns NULL. When viewing the entity obtained through Kint debug, my data is visible in the attributes. Also, when using getters or setters of an entity, I constantly get an error
Quote:Call to a member function test_func () on array

An entity object is returned by the model. I did everything according to the CI4 documentation, but ran into such a problem. What are the options?
Reply

#2
I think showing everyone a little bit more code would make possible reasonable replies.
Reply

#3
The sequence of my actions:
1. Create a model

Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class AdminModel extends Model
{
        protected $table      = 'admin';

        protected $returnType = 'App\Entities\Admin';

        protected $allowedFields = ['last_login', 'last_logout', 'last_ip'];
}


2. Create an entity

Code:
<?php namespace App\Entities;

use CodeIgniter\Entity;

class Admin extends Entity
{

    public function getPassword(){
        return $this->attributes['password'];
    }
   
    public function verify_pass(string $user_password){
        return password_verify($user_password, $this->password);
    }
   
}


3. Initialize the model in the controller.

Code:
$AdminModel = new AdminModel();

4. Trying to get data from the entity.



Code:
$admin = $AdminModel->where('username', $this->request->getPost('username'))
                            ->findAll();

if(empty($admin))
            return redirect()->back()->with('error', array('auth_error' => 'User not found!'));

if( ! $admin->verify_pass($this->request->getPost('password')))
            return redirect()->back()->with('error', array('auth_error' => 'Password incorrect!'));

In case of calling an entity method, I get an error:
Quote:Call to a member function verify_pass() on array





When accessing entity fields directly, the resulting value = NULL. And in the dd output, my data is visible.
   
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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