cakephp: Missing virtual fields and getters on associated entities

After upgrading my Cake 3.1.x project to 3.2.x, I can no longer get values besides (null) for virtual properties made through getter functions on my find() associated entities.

Here is a simple example of my problem using my Patients model finding their Client association.

Controller code:

    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {   
        $this->paginate['contain'] = [
            'Clients'
        ];
        $patients = $this->Patients->find('all');

        $this->set('patients', $this->paginate($patients));
        $this->set('_serialize', ['patients']); 
    }

Here is the virtual property I have set in my Client entity:

    /**
     * Exposed virtual fields
     *
     * @var array
     */
    protected $_virtual = [
        'ListIdName'
    ];   

    protected function _getListIdName()
    {
        return $this->_properties['OrganizationName'] . ' [' . $this->_properties['ClientId'] . ']';
    } 

This is returning (null) for $patient->client->listIdName which I can’t seem to figure out.

I have tried changing the getter functions to public scope, which does not help. I’ve also tried using the paginate() method after adding the contain() to the original query as opposed to in $this->paginate[]. The result keeps coming back null.

Here is the relevant output of pr($patients->first()) on my query before pagination:

App\Model\Entity\Patient Object
(
    [PatientId] => 1
    [ClientId] => 2
    [FirstName] => First
    [LastName] => Last
    [client] => App\Model\Entity\Client Object
        (
            [ClientId] => 2
            [OrganizationName] => Some Client
            [[new]] => 
            [[accessible]] => Array
                (
                    [*] => 1
                )

            [[dirty]] => Array
                (
                )

            [[original]] => Array
                (
                )

            [[virtual]] => Array
                (
                    [0] => ListIdName
                )

            [[errors]] => Array
                (
                )

            [[invalid]] => Array
                (
                )

            [[repository]] => Clients
        )

    [[new]] => 
    [[accessible]] => Array
        (
            [*] => 1
        )

    [[dirty]] => Array
        (
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
            [0] => FullName
            [1] => ListName
            [2] => ListIdName
        )

    [[errors]] => Array
        (
        )

    [[invalid]] => Array
        (
        )

    [[repository]] => Patients
)

As you can see, the virtual fields are not there. These worked in CakePHP 3.1.10 but are not working in 3.2.1 or 3.2.2 that was just released.

I didn’t see anything in the changelog about this functionality changing so I’m hoping this is just a bug and not something I’m doing wrong. Any help would be greatly appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@holisticnetworking Knowing all the virtual properties on an entity would require non-free reflection if we didn’t have the explicit list, as virtual fields aren’t defined anywhere other than by implementing methods via a naming convention.

Thanks for the explanation.

VirtualFields have always required to be listed in the $_virtual property if you want them to be in array conversions.