Virtual Fields with Cakephp 3

I need to have a virtual property in my user entity. I followed the CakePHP book .

UserEntity.php

namespace App\Model\Entity; use Cake\ORM\Entity; class User extends Entity { protected $_virtual = ['full_name']; protected function _getFullName() { return $this->_properties['firstname'] . ' ' . $this->_properties['lastname']; } } 

In the controller

 $users = TableRegistry::get('Users'); $user = $users->get(29); $firstname = $user->firstname; // $firstname: "John" $lastname = $user->lastname; // $lastname: "Doe" $value = $user->full_name; // $value: null 

I followed this particular book, and I only get null .

+6
source share
2 answers

According to @ndm, the problem was caused by an invalid file name. I called the user entity class UserEntity.php . CakePHP naming conventions say that:

The Entity OptionValue class will be found in a file named OptionValue.php.

Thanks.

+7
source

Why not just go for something like that?

 /* * Return Fullname */ public function getFullname() { $name = $this->firstname . ' ' . $this->lastname; return $name; } 
0
source

All Articles