php - 如何在不加入 doctrine2 的情况下获得 id?

标签 php doctrine-orm

我有这样的实体:

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

在纯 SQL 中我可以

SELECT * FROM table WHERE id = 100

并在不加入用户表的情况下获取 userId。

最佳答案

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

例子:

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-functions

关于php - 如何在不加入 doctrine2 的情况下获得 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3913150/

相关文章:

php - 使用 PHP 和 MYSQL 创建数据透视表 - 向左添加列

php - 喜欢一个带有 selenium 的 youtube 评论

sql - symfony2 - Doctrine - 如何使用计数和分组进行多重选择

php - Symfony2,Doctrine2 在关联 sth#category 上找到类型为 Doctrine\Common\Collections\ArrayCollection 的实体,但期待 sth

php - xampp 需要很长时间才能进行大型查询

php - 依赖注入(inject)容器 PHP

php - $_POST 错误

mysql - symfony2 : MySQL server error when trying to create a database

php - 映射相互不一致

php - 使用 Native SQL 的 Doctrine 查询总是返回空数组