schrodingerscatcuriosity
schrodingerscatcuriosity

Reputation: 1860

Cake php table join two fields

I'm starting with php using Cakephp 4.x.

I baked a table called Persona (spanish for person), with two fields, last_names and names.

I set the setDisplayField in my PersonaTable to last_names but I want to join it with names, so the output would be:

Doe, John

The relevant code:

public function initialize(array $config): void
{
    parent::initialize($config);

    $this->setTable('personas');
    
    /* 
     * here I want to join last_names with names
     */
    $this->setDisplayField('last_names');
    $this->setPrimaryKey('id');
}

My guess is that I have to create a function in my PersonaTable that gives that result, but I don't know where to start.

Upvotes: 0

Views: 112

Answers (1)

schrodingerscatcuriosity
schrodingerscatcuriosity

Reputation: 1860

The answer is Virtual entity fields:

Adding to the model this function

protected function _getFullName() {
    return $this->first_name . ' ' . $this->last_name;
}

I can access full_name as a field, like:

$persona->full_name

Upvotes: 2

Related Questions