Virtual Field Entity

Hi.

I want to create a virtual field in an entity Ciudade for use it in setDisplayField in Ciudadestable… But I need acces the nombre_departamento located in Departamentos table.

Any way?

protected function _getCiudadDepartamento()
{
return
$this->nombre_ciudad .
’ - ’ .
$this->Ciudades->Departamentos->nombre_departamento;
}

Your question isn’t specific enough. Is Ciudade linked to Departamentos (belongsTo, hasMany, etc). If linked, then just add a contain and use something $this->Departamentos->nombre_departamento

Maybe calling TableRegistry::getTableLocator()->get('Departamentos'); to get the table, and then get your data.

An alternative. Maybe the formatResults feature may help you. Docs

I share the way I did solved this:

Entity Ciudade.php

protected function _getCiudadDepartamento()
{
return
$this->nombre_ciudad .
’ - (’ .
$this->departamento->nombre_departamento .
‘)’ ;
}

Ciudadestable.php
$this->setDisplayField([‘ciudad_departamento’]);

in the controller
$ciudadEjecucionContratos = $this->Contratos->Ciudades->find(‘list’)->contain(‘Departamentos’);

Thanks.