PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 5, 2022

[FIXED] Virtual field does not appear in REST JSON result

 January 05, 2022     cakephp, cakephp-3.0, orm, rest     No comments   

Issue

I have a Model named Users with a virtual field named full_name, and it works fine in the template by calling:

<?= $user->full_name; ?>

But it doesn't automatically come to the REST.


Solution

Quote from the docs:

By default virtual properties are not exported when converting entities to arrays or JSON. In order to expose virtual properties you need to make them visible. When defining your entity class you can provide a list of virtual properties that should be exposed:

namespace App\Model\Entity;

use Cake\ORM\Entity;

class User extends Entity
{

    protected $_virtual = ['full_name'];

}

This list can be modified at runtime using virtualProperties:

$user->virtualProperties(['full_name', 'is_admin']);

Cookbook > Database Access & ORM > Entities > Exposing Virtual Fields



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing