虚拟字段与Cakephp 3 [英] Virtual fields with Cakephp 3

查看:100
本文介绍了虚拟字段与Cakephp 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的用户实体中有一个虚拟属性。我遵循 CakePHP图书

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

UserEntity.php


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'];
    }
}

/ p>

In a 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

我完全按照这本书,我只得到一个 null value。

I followed exactly the book and I only get a null value.

推荐答案

根据@ndm,问题是由于文件命名错误。我命名了用户实体类 UserEntity.php CakePHP名称约定说:

According to @ndm, the problem was due to a bad file naming. I named the user entity class UserEntity.php. The CakePHP name conventions says that:


实体类OptionValue将在名为OptionValue.php的文件中找到。

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

谢谢。

这篇关于虚拟字段与Cakephp 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆