如何在没有加入doctrine2的情况下获取id?

时间:2022-12-23 06:49:07

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()

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

doctrine generate query like:

doctrine生成查询,如:

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.

但如果我不需要表用户,如何获得userId。

In pure SQL i can just

在纯SQL中我可以

SELECT * FROM table WHERE id = 100

and get userId without join users table.

并获取没有join users表的userId。

4 个解决方案

#1


28  

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

您可能还想查看IDENTITY()函数(Doctrine版本> 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

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

#2


5  

Try this:

$q = $qb->getQuery();
$q->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);

#3


0  

According to the documentation, D2 entities should never need the foreign key exposed as it is all done internally by the mapping information, however, as you have found out, there is a need now and then to have this value. To do this, simply supply the mapping info for the foreign key.

根据文档,D2实体永远不需要暴露外键,因为它全部由映射信息在内部完成,但是,正如您所知,有时需要具有此值。为此,只需提供外键的映射信息。

#4


-4  

I don't know doctrine2 but from what I read this is a ORM. So I would suggest that you need a form of lazy loading where user isn't loaded. This article suggests that lazy loading is available in doctrine2.

我不知道doctrine2,但从我读到的这是一个ORM。所以我建议您需要一种延迟加载的形式,用户不会加载。本文建议在doctrine2中提供延迟加载。

This may result in multiple db calls if user is later used on the returned table. You have to weigh this up with the idea of grabbing the data in one hit.

如果稍后在返回的表上使用用户,则可能导致多个db调用。你必须权衡这一点,以便在一次点击中获取数据。

#1


28  

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

您可能还想查看IDENTITY()函数(Doctrine版本> 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

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

#2


5  

Try this:

$q = $qb->getQuery();
$q->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);

#3


0  

According to the documentation, D2 entities should never need the foreign key exposed as it is all done internally by the mapping information, however, as you have found out, there is a need now and then to have this value. To do this, simply supply the mapping info for the foreign key.

根据文档,D2实体永远不需要暴露外键,因为它全部由映射信息在内部完成,但是,正如您所知,有时需要具有此值。为此,只需提供外键的映射信息。

#4


-4  

I don't know doctrine2 but from what I read this is a ORM. So I would suggest that you need a form of lazy loading where user isn't loaded. This article suggests that lazy loading is available in doctrine2.

我不知道doctrine2,但从我读到的这是一个ORM。所以我建议您需要一种延迟加载的形式,用户不会加载。本文建议在doctrine2中提供延迟加载。

This may result in multiple db calls if user is later used on the returned table. You have to weigh this up with the idea of grabbing the data in one hit.

如果稍后在返回的表上使用用户,则可能导致多个db调用。你必须权衡这一点,以便在一次点击中获取数据。