Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an id without join in doctrine2?

I have entity like this:

/**  *  * @Table(name="table")  * @Entity  */  class Table {      /**      * @Column(type="integer")      * @Id      * @GeneratedValue(strategy="IDENTITY")      */      private $id;       /**      * @ManyToOne(targetEntity="Entities\Users")      * @joinColumn(name="userId", referencedColumnName="id")      */      private $User;       /**      * @Column(type="string")      */      private $text;   } 

If i do $q->getQuery()->getSingleResult()->getUser()->getUserId()

doctrine generate query like:

SELECT * FROM table t INNER JOIN users u ON u.id = t.userId WHERE id = 100 

but if i don`t need table users, how to get an userId.

In pure SQL i can just

SELECT * FROM table WHERE id = 100 

and get userId without join users table.

like image 609
Artem Bondarenko Avatar asked Oct 12 '10 09:10

Artem Bondarenko


1 Answers

You may also want to look at the IDENTITY() function (Doctrine version >2.2).

Example:

SELECT IDENTITY(t.User) AS user_id from Table 

Should return:

[ ['user_id' => 1], ['user_id' => 2], ... ] 

See also: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-functions

like image 196
Tarjei Huse Avatar answered Sep 23 '22 23:09

Tarjei Huse