P
P
Pavel Osipov2012-08-25 15:19:25
symfony
Pavel Osipov, 2012-08-25 15:19:25

Symfony2 + Doctrine. How to get @ORM\ManyToOne relation with a condition?

Good day
There was a problem, I ask for help from people experienced in Symfony.
There are two tables Offer and CustomerFiles
. CustomerFiles stores all files uploaded by the user, in particular, files for offers. The file type is determined by two fields:
file_type
target_id
I.e. for Offer with ID=22 their values ​​will be
file_type = 'offer_image'
target_id = 22
At the moment I have described the relationship like this:
For Offer

/**<br>
     * @ORM\OneToMany(targetEntity="CustomerFiles", mappedBy="offer", cascade={"all"}, orphanRemoval=true  )<br>
     */<br>
    protected $files;<br>

For CustomerFiles
/**<br>
     * @ORM\ManyToOne(targetEntity="Offer", inversedBy="files")<br>
     * @ORM\JoinColumn(name="target_id", referencedColumnName="id")<br>
     */<br>
    protected $offer;<br>

I get the Offer myself simply:
$offer = $em -> getRepository( 'ProjectMainBundle:Offer' )<br>
                -> findOneById( $offerID );<br>

But in the end, all files with target_id=22 are selected.
How to add WHERE condition file_type = 'offer_image'?
Or is it impossible to do this with the help of annotations and you need to create a repository and write a raw DQL query to get an Offer with all Joins?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Plisko, 2012-08-26
@Pavel_Osipov

Since you have files of different types, then start different types for them in the same table. In vain you biked with file_type
doctrine-orm.readthedocs.org/en/2.0.x/reference/dql-doctrine-query-language.html#single-table
Well, then write the links to the desired Entity.

S
Sergey, 2012-08-25
Protko @Fesor

In general, they usually do this: they create their own repository and either add a request there, or overload the findBy method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question