Cakephp 3的虚拟字段

前端之家收集整理的这篇文章主要介绍了Cakephp 3的虚拟字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在我的用户实体中拥有一个虚拟属性.我跟着 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'];
    }
}

在控制器中

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

我完全按照这本书,我只得到一个空值.

根据@ndm,问题是由于文件命名错误.我将用户实体类命名为UserEntity.PHP. The CakePHP name conventions说:

The Entity class OptionValue would be found in a file named OptionValue.PHP.

谢谢.

原文链接:https://www.f2er.com/php/130393.html

猜你在找的PHP相关文章