0

I already have some virtual fields working on my application, but this one is busting my mind.

So I have the entity SubactivitySlots, with the following code:

protected $_virtual = [
    'slots_text',
];

and

protected function _getSlotsText(){
    return "test";
}

When I run the query:

debug($this->SubactivitySlots->find('all')->first());

It returns me the following structure (tried with both first and toArray()

object(App\Model\Entity\SubactivitySlot) {

'id' => (int) 1,
'name' => 'MAIN',
'description' => '-',
'activity_id' => (int) 1,
'subactivity_min' => (int) 1,
'subactivity_max' => (int) 1,
'position' => (int) 1,
'institution_id' => (int) 1,
'deleted' => (int) 0,
'[new]' => false,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [
    (int) 0 => 'slots_text'
],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'SubactivitySlots'

}

Any clue of what might be wrong? I'm spent a lot of time trying to discover and can't find out. The strangest thing is that i'm using virtual fields on another entities and it's working.

Thanks

Cafn
  • 137
  • 1
  • 8
  • 26

1 Answers1

2

If I'm not wrong cake does not debug virtual fields until 3.5.13.

In cake 3.4 the $_virtual property is used when "converting entities to arrays or JSON" (from here) but not in debug mode.

So even if you don't see them when you debug you should see them when uou convert the entity in JSON

Anyway this should have changed in 3.5.13 (see the blog here) so if you can upgrade the issue will be solved

arilia
  • 9,373
  • 2
  • 20
  • 44