Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Mysql queries with the table name and "AS" for every column.

This code:


$user = User::findFirst(
           [
               'email=:email:',
               'bind' => [
                   ':email' => $email
               ],
               'columns' => 'id,  password'
           ]
       );

Produces this query:


SELECT `users`.`id` AS `id`, `users`.`password` AS `password`  FROM `users` WHERE `users`.`email` = :email LIMIT :2

Why it adds the table name and "AS" for every column? It's supposed to work like that or i need to modify something in the model or configuration?

The model is very basic since i'm just starting out.


class User extends \Phalcon\Mvc\Model
{

    public $id;

    public $email;

    public function getSource()
    {
        return "users";
    }
}

Thank you



5.9k
Accepted
answer

There's no issue with the 'AS' keyword being used

In reference to the 'AS' keyword in the SELECT clause, it can be used for:

  • Shortening the overall length of a query and making it look neater
  • Avoiding column name colisions when the query incorperates multiple tables

tl;dr: It's just the way the ORM works.



1.1k

Hello Mitchel.

Thank you very much for the explanation!



19.3k

Hi,

Is there a way we can change the column name like in a select clause.

i.e.

Class::find(array( 'conditions' => 'ID = 1', 'columns' => 'Name AS FullName' <---- This is what I try to achieve ));

Thank you.

You can do 'column mapping' on a per-model basis, I haven't personally checked if PHQL is compatible with it, but I think it's almost say to say it would be.

For some reason the documentation for column mapping is missing in the latest version, so here's a link to v1 documentation describing how to set it up. https://phalcon-php-framework-documentation.readthedocs.io/en/1.0.0/reference/models.html?highlight=columnMap#independent-column-mapping



19.3k

Just found the answer by my self. Just use

'columns' => 'columns as aliasname'

Thank you