【问题标题】:Symfony Doctrine Query Builder. How to select value of many-to-one field without join?Symfony 学说查询生成器。如何在不连接的情况下选择多对一字段的值?
【发布时间】:2018-09-12 08:48:08
【问题描述】:

我有两个实体:

 class Products
 {
    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     */
    private $id;

     /**
      * @ORM\Column(name="name", type="string", length=255, nullable=true)
      */
     private $name;

     /**
      * @var CatalogSections|null $catalogSection
      * @ORM\ManyToOne(targetEntity="App\Entity\CatalogSections")
      * @ORM\JoinColumn(name="catalog_section", referencedColumnName="id")
      */
     private $catalogSection; 

     ...
}

和目录部分

class CatalogSections
{
    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string", length=250, nullable=true)
     */
    private $name;

    ...
}

我想通过 Doctrine Query Builder 获取产品的 catalog_section ID。有没有不加入的方法获得它? IE。我想获取存储在产品表中的 catalog_section 列值

$qb = $this->createQueryBuilder('products');

$qb->select('products.id', 'products.name',
    'products.catalogSection' // catalog_section 
);

【问题讨论】:

标签: symfony doctrine


【解决方案1】:

选择没有 Join 的 ID 的唯一正当理由是为了提高性能。

当考虑性能时,最好直接使用DBAL Connection 而不是查询生成器。

$this->entityManager->getConnection()->executeQuery('SELECT id, name, catalog_section FROM products')

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 2011-11-03
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多