Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - Column aliases in SQL queries

Tags:

sql

model

cakephp

Is there a good way to specify column aliases when performing find() operations on a model?

$this->User->find('first', array(
    'fields' => array(
        'CONCAT(firstname, ' ', surname) AS fullname',
        'email',
        'tel'
    )
);

At the moment, if I do it like this it returns the data like this:

Array
(
    [0] => Array
        (
            [fullname] => John Smith
        )

    [User] => Array
        (
            [email] => [email protected]
            [tel] => 0123456789
        )

)

Is there a way to have it return the column aliases like normal columns?

Array
(
    [User] => Array
        (
            [fullname] => John Smith
            [email] => [email protected]
            [tel] => 0123456789
        )

)
like image 874
BadHorsie Avatar asked Feb 23 '23 11:02

BadHorsie


1 Answers

For this you're supposed to use Virtual Fields.

like image 180
deceze Avatar answered Feb 26 '23 10:02

deceze