• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access entity method?

#1
Hello.

I've followed myth's auth extend guide to add some fields https://github.com/lonnieezell/myth-auth...tending.md

I have this in my entity:
Code:
<?php namespace App\Entities;

use Myth\Auth\Models\UserModel as MythUser;

class User extends MythUser
{
    /**
     * Default attributes.
     * @var array
     */
    protected $attributes = [
        'firstname' => 'Usuario',
        'lastname'  => 'Invitado',
    ];

    /**
     * Returns a full name: "first last"
     *
     * @return string
     */
    public function getName()
    {
        return trim(trim($this->attributes['firstname']) . ' ' . trim($this->attributes['lastname']));
    }
}

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

use Myth\Auth\Models\UserModel as MythModel;

class UserModel extends MythModel
{
    protected $returnType = 'App\Entities\User';
    protected $allowedFields = [
        'email', 'password_hash', 'reset_hash', 'reset_at', 'reset_expires', 'activate_hash',
        'status', 'status_message', 'active', 'force_pass_reset', 'permissions', 'deleted_at',
        'firstname', 'lastname', 'phone',
    ];

    protected $validationRules = [
        'email'         => 'required|valid_email|is_unique[users.email,id,{id}]',
        'firstname'     => 'required|alpha_numeric_punct|min_length[3]',
        'lastname'      => 'required|alpha_numeric_punct|min_length[3]',
        'phone'            => 'required|min_length[7]',
        'password_hash' => 'required',
    ];
}

So... according doc I could do $user->name to access entity method but in my controller I have:
Code:
<?php namespace App\Controllers\App;

use App\Controllers\BaseController;

class Users extends BaseController
{   
    public function view( $user_id=false )
    {
        $model = model('UserModel');
        $user = $model->find();
        d($user->name);
    }
    //--------------------------------------------------------------------
}

In kint I can see getName as an available method but I got this error:

"Trying to get property 'name' of non-object"

How can I acces that method?


Attached Files Thumbnail(s)
   
Reply

#2
(11-15-2020, 01:19 PM)jnar Wrote:
Code:
    public function view( $user_id=false )
    {
        $model = model('UserModel');
        $user = $model->find();
        d($user->name);
    }

In kint I can see getName as an available method but I got this error:

"Trying to get property 'name' of non-object"

How can I acces that method?

That's because find() doesn't find anything. You need to pass the id as parameter:
PHP Code:
    public function view$user_id=false )
    {
        $model model('UserModel');
        $user $model->find($user_id);
        d($user->name);
    
Reply

#3
(11-15-2020, 04:52 PM)includebeer Wrote: That's because find() doesn't find anything. You need to pass the id as parameter:
PHP Code:
    public function view$user_id=false )
    {
        $model model('UserModel');
        $user $model->find($user_id);
        d($user->name);
    

Thanks for your answer but still wont work, I get this:
Code:
$user->name null
Reply

#4
But you no longer have "Trying to get property 'name' of non-object" ?
Reply

#5
(11-15-2020, 06:01 PM)includebeer Wrote: But you no longer have "Trying to get property 'name' of non-object" ?

Well you are right on that... Anyways I found my answer with help of another thread.  I needed to extend original entity, not model as I was doing.

I had to change this (was taken literally from docs) on my entity:
Code:
use Myth\Auth\Models\UserModel as MythUser;

For this:
Code:
use \Myth\Auth\Entities\User as MythEntity;

Then put this on my controller:
Code:
public function view( $user_id=false )
{
    $model = model('UserModel');
    $data['user'] = $model->find($user_id);

    echo view('user\profile',$data);
}

And finally in my view I were able to do this:
Code:
<h3 class="profile-username text-center"><?= $user->name ?></h3>

And all is good now.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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