Vladimir
Vladimir

Reputation: 28

Symfony Doctrine Query Builder. How to select value of many-to-one field without join?

I have two entities:

 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; 

     ...
}

And catalog sections

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;

    ...
}

I want to get catalog_section ID of Product via Doctrine Query Builder. Is there a way to get it without join? I.E. i want to get a catalog_section column value which store in products table

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

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

Upvotes: 0

Views: 1703

Answers (1)

j_elfering
j_elfering

Reputation: 3190

The only valid reason to select the IDs without a Join is to gain performance.

And when performance is concerned it is best to use the DBAL Connection directly instead of the query builder.

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

Upvotes: 1

Related Questions