1

I am trying to construct an object with two composite foreign keys pointing out to the same object, but they seem to have the same data, like doing the join only on one column, product_id.

class PostpaidProduct extends Product {
    /**
     * @ManyToOne(targetEntity="Bundle", fetch="EAGER", cascade={"persist"})
     * @JoinColumn(name="bundle_voice_id", referencedColumnName="id")
     */
    private $bundleVoice;

    /**
     * @ManyToOne(targetEntity="Bundle", fetch="EAGER", cascade={"persist"})
     * @JoinColumn(name="bundle_data_id", referencedColumnName="id")
     */
    private $bundleData;

    /**
     * @OneToMany(targetEntity="BundlePromo", mappedBy="product", fetch="EAGER", cascade={"persist"})
     * @JoinColumns({
     *   @JoinColumn(name="id", referencedColumnName="product_id"),
     *   @JoinColumn(name="bundle_voice_id", referencedColumnName="bundle_id")
     * })
     */
    private $bundleVoicePromos;

    /**
     * @OneToMany(targetEntity="BundlePromo", mappedBy="product", fetch="EAGER", cascade={"persist"})
     * @JoinColumns({
     *   @JoinColumn(name="id", referencedColumnName="product_id"),
     *   @JoinColumn(name="bundle_data_id", referencedColumnName="bundle_id")
     * })
     */
    private $bundleDataPromos;

}

What would be wrong with my mapping? Is it possible to have composite foreign keys but without being primary keys?

madalex
  • 166
  • 1
  • 10
  • what's the error ? Btw @Id is required; you should add on each properties of your composite key. – DEY Apr 09 '13 at 09:12

1 Answers1

0

I have talked to one of the developers of Doctrine and he said that the @JoinColumns field in @OneToMany relationships is ignored. The alternative would be having just one foreign key and to use matching criterias in an entity method, filtering for the entries needed based on the other key. Another solution would be having repository methods specific for getting these values.

Also, in OneToMany relationships eager fetching does not work, so it does separate queries for all children. So if you have a product with multiple prices, when fetching a product it will do separate queries for fetching the prices.

madalex
  • 166
  • 1
  • 10
  • Not true. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#manytoone there is `fetch="EAGER"` option in doctrine. And also JoinColumn is never *ignored* not sure where you got this information from – some_groceries Mar 18 '17 at 12:55
  • it was 4 years ago – madalex Mar 19 '17 at 14:40