Answer the question
In order to leave comments, you need to log in
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>
/**<br>
* @ORM\ManyToOne(targetEntity="Offer", inversedBy="files")<br>
* @ORM\JoinColumn(name="target_id", referencedColumnName="id")<br>
*/<br>
protected $offer;<br>
$offer = $em -> getRepository( 'ProjectMainBundle:Offer' )<br>
-> findOneById( $offerID );<br>
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question