How do you define a "one to many" doctrine association that allows the user to add a new "one"?

时间:2015-09-11 15:14:22

标签: symfony doctrine-orm one-to-many

I'm trying to define my doctrine entities for use with Symfony2 and am am unclear how about to define an association. The below example is for customers compiling a wishlist of components they would like for a custom-made bicycle.

A BikeEnquiry object can:

  • only ever take one frame
  • by default takes two wheels (but could be zero or more)

However, regarding the accessories, I'm less sure about how these should be mapped.

The user should be able to select from a dropdown of accessories to see all the items we have in the database - however, they should also be able to manually enter a description of what they are looking for if it does not exist (such as a new product on the market). I would like this new accessory then be added to the Accessory database table - it would also be good to have it flagged up as an unvalidated item for my review.

Below is my attempt as defining the entities and annotation associations:

use Doctrine\Common\Collections\ArrayCollection;

/** @Entity **/
class BikeEnquiry 
{
    private $BikeEnquiryid;
    private $frameid;

   /**
    * @OneToMany(targetEntity="Wheel", mappedBy="wheelid")
    **/
    private $wheelid;

   /**
    * @ManyToMany(targetEntity="Accessory")
    * @JoinTable(name="BikeEnquires_accessories",
    *      joinColumns={@JoinColumn(name="BikeEnquiryid", referencedColumnName="BikeEnquiryid")},
    *      inverseJoinColumns={@JoinColumn(name="accessoryid", referencedColumnName="accessoryid", unique=true)}
    *      )
    **/ 
    private $accessories;

    public function __construct() {
        $this->accessories = new ArrayCollection();
    }
}

/** @Entity **/
class Frame
{
   /**
    * @OneToOne(targetEntity="BikeEnquiry")
    * @JoinColumn(name="BikeEnquiryid", referencedColumnName="BikeEnquiryid")
    **/
    private $frameid;
}

/** @Entity **/
class Wheel
{
   /**
    * @ManyToOne(targetEntity="BikeEnquiry")
    * @JoinColumn(name="BikeEnquiryid", referencedColumnName="BikeEnquiryid")
    **/
    private $wheelid;
}

/** @Entity **/
class Accessory
{
    private $accessoryid;
}

0 个答案:

没有答案