如何获取id而不加入doctrine2? [英] How to get an id without join in doctrine2?

查看:96
本文介绍了如何获取id而不加入doctrine2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的实体:

  / ** 
*
* @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;


}

如果我做
$ q-> getQuery() - > getSingleResult() - > getUser() - > getUserId()



doctrine生成查询,如:

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

但如果我不需要表用户,如何获取一个userId。 p>

在纯SQL中,我只能

  SELECT * FROM table WHERE id = 100 

并获取userId而不加入用户表。

解决方案

您可能还想查看IDENTITY()函数(Doctrine版本> 2.2)。



示例: / p>

  SELECT IDENTITY(t.User)AS user_id from Table 

应该返回:

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

另请参见:
http://docs.doctrine- project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-功能


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.

解决方案

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

这篇关于如何获取id而不加入doctrine2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆