Multiple JoinColumns in Symfony2 using Doctrine annotations?

前端 未结 1 1043
故里飘歌
故里飘歌 2021-01-14 20:55

Here is the problem:

Class Routing with attributes objectId and objectType. objectId is an int, and objectT

相关标签:
1条回答
  • 2021-01-14 21:26

    After further researching I found a man (Dirk Olbertz) in the know who had the same problem.

    Information can be found at: Google Groups: Multiple JoinColumns?

    I have now implemented this and I will explain how I did it incase it might help anyone else.

    The answer to my problem was the use of single table inheritance.

    The first thing I needed to do was update the routing entity to use single table inheritance:

    ../Entity/Routing.php

    /**
     * @ORM\Entity
     * @ORM\InheritanceType("SINGLE_TABLE")
     * @ORM\DiscriminatorColumn(name="object_type", type="string")
     * @ORM\DiscriminatorMap({"product" = "ProductRouting", "department" = "DepartmentRouting", "brand" = "BrandRouting"})
     * @ORM\Table(name="routing")
     */
    class Routing
    {
        /**
         * @ORM\Id
         * @ORM\Column(name="id", type="integer", length=11)
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    ...
    

    The DiscriminatorColumn allowed me to specify what column would be used to link, which in this case was the object_type field.

    The DiscriminatorMap allowed me to specify what object_type will link with what entities.

    These entities then had to be created that extended the Routing entity.

    ../Entity/ProductRouting.php

    /**
     * @ORM\Entity
     */
    class ProductRouting extends Routing
    {
        /**
         * @ORM\ManyToOne(targetEntity="Product")
         * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
         */
        protected $product;
    ...
    

    ../Entity/DepartmentRouting.php

    /**
     * @ORM\Entity
     */
    class DepartmentRouting extends Routing
    {
        /**
         * @ORM\ManyToOne(targetEntity="Department")
         * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
         */
        protected $department;
    ...
    

    ../Entity/BrandRouting.php

    /**
     * @ORM\Entity
     */
    class BrandRouting extends Routing
    {
        /**
         * @ORM\ManyToOne(targetEntity="Brand")
         * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
         */
        protected $brand;
    ...
    

    Then in each of the Product, Department and Brand entities I needed to add the new $routings.

    ../Entity/Product.php

    ...
    class Product
    {
        ...
        /**
         * @ORM\OneToMany(targetEntity="ProductRouting", mappedBy="product", cascade={"all"})
         */
        private $routings;
    ...
    

    ../Entity/Department.php

    ...
    class Department
    {
        ...
        /**
         * @ORM\OneToMany(targetEntity="DepartmentRouting", mappedBy="department", cascade={"all"})
         */
        private $routings;
    ...
    

    ../Entity/Brand.php

    ...
    class Brand
    {
        ...
        /**
         * @ORM\OneToMany(targetEntity="BrandRouting", mappedBy="brand", cascade={"all"})
         */
        private $routings;
    ...
    

    Hope that helps...

    0 讨论(0)
提交回复
热议问题