加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

Cakephp 3的虚拟字段

发布时间:2020-05-31 00:42:12 所属栏目:PHP 来源:互联网
导读:我需要在我的用户实体中拥有一个虚拟属性.我跟着 CakePHP book. UserEntity.php namespace AppModelEntity;use CakeORMEntity;class User extends Entity { protected $_virtual = [full_name]; protected functi

我需要在我的用户实体中拥有一个虚拟属性.我跟着 CakePHP book.

UserEntity.php

namespace AppModelEntity;

use CakeORMEntity;

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.

谢谢.

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读