Search code examples
doctrinesymfonydql

Join table in Doctrine without Entity


I have a problem which I can't solve. I have an entity, to which I'm trying to join a table:

 /**
     * @var ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="FeaturedAttribute")
     * @ORM\JoinTable(name="property_featured_attribute",
     *     joinColumns={@ORM\JoinColumn(name="property", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="featured_property", referencedColumnName="id")})
     */

I need to join table table with name property_featured_attribute in my DQL-code:

LEFT JOIN property_featured_attribute pfa WITH pfa.property = p.id
WHERE pfa.featured_property IN (:attrs)

The problem is, that this table - property_featured_attribute - hasn't Entity, so I get an error like:

'Error: Class 'property_featured_attribute' is not defined.'

Is it possible to join table without entity in my DQL-query?


Solution

  • You can't do this using DQL, because purpose of DQL is to make queries over your object model. DQL is a query language for your object model, not for your relational schema.

    But you can make plain query using DBAL:

    $conn = $this->get('database_connection');
    $res = $conn->query('SELECT...');