开发者

CakePHP - Column aliases in SQL queries

开发者 https://www.devze.com 2023-03-23 05:23 出处:网络
Is there a good way to specify column aliases when performing find() operations on a model? $this->User->find(\'first\', array(

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] => jsmith@example.co开发者_JS百科m
            [tel] => 0123456789
        )

)

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

Array
(
    [User] => Array
        (
            [fullname] => John Smith
            [email] => jsmith@example.com
            [tel] => 0123456789
        )

)


For this you're supposed to use Virtual Fields.


For cakephp 2.x in query:

'CONCAT(firstname, ' ', surname) AS User__fullname',

http://book.cakephp.org/2.0/en/models/virtual-fields.html#virtual-fields-in-sql-queries

0

精彩评论

暂无评论...
验证码 换一张
取 消