How to access entity method? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: How to access entity method? (/showthread.php?tid=77982) |
How to access entity method? - jnar - 11-15-2020 Hello. I've followed myth's auth extend guide to add some fields https://github.com/lonnieezell/myth-auth/blob/develop/docs/extending.md I have this in my entity: Code: <?php namespace App\Entities; Model: Code: <?php namespace App\Models; So... according doc I could do $user->name to access entity method but in my controller I have: Code: <?php namespace App\Controllers\App; 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? RE: How to access entity method? - includebeer - 11-15-2020 (11-15-2020, 01:19 PM)jnar 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 ) RE: How to access entity method? - jnar - 11-15-2020 (11-15-2020, 04:52 PM)includebeer Wrote: That's because find() doesn't find anything. You need to pass the id as parameter: Thanks for your answer but still wont work, I get this: Code: $user->name null RE: How to access entity method? - includebeer - 11-15-2020 But you no longer have "Trying to get property 'name' of non-object" ? RE: How to access entity method? - jnar - 11-15-2020 (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 ) 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. |