php - Cakephp 3 的虚拟字段

标签 php cakephp orm cakephp-3.0

我需要在我的用户实体中拥有一个虚拟属性(property)。我关注了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'];
    }
}

在 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值。

最佳答案

根据@ndm 的说法,问题是由于错误的文件命名造成的。我将用户实体类命名为 UserEntity.phpThe CakePHP name conventions说:

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

谢谢。

关于php - Cakephp 3 的虚拟字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437227/

相关文章:

hibernate - 如何仅检索 JPQL 或 HQL 中实体的某些字段? JPQL 或 HQL 中 ResultSet 的等价物是什么?

javascript - 每隔一段时间调用 JQuery ajax 不会重定向页面

php - 如何在 MySQL 中比较两个数字作为字符串?

node.js - 如何获取 dgraph-orm 中查询的 ' logs'

cakephp - fetch 函数如何在 cakephp 布局 View 中工作

ajax - 蛋糕PHP 1.3 : Detect ajax request in view

python - "ProgrammingError: column "genre_id "of relation "music_album "does not exist"而该列确实存在

php - 获取虚拟机中托管的用户 IP 地址网站

javascript - 评论仅适用于第一篇文章

php - 使用 HABTM 查找未显示 CakePHP 中引用表结果的查询