Conditional Relations with the Symfony Doctrine

I need to create this database schema in Symfony Entities:

ClassA:

  • ID
  • name
  • ... some other attributes only for class A

ClassB:

  • ID
  • name
  • ... some other attributes only for class B

ClassC:

  • ID
  • name
  • ... some other attributes only for class C

Course:

  • ID
  • name
  • of type
  • class_id
  • class_type [A, B, C]
  • ...

I need to create a relationship between the course Entity (using class_id) with other objects (ClassA, ClassB, ClassC) (using the identifier) ​​and using class_type (A, B, C).

A course is not a class, and a class is not a course. There is no inheritance. In my project, I use this concept (id and type to display different objects). So I need to solve this problem with a simple example like this

I thought about using @ORM \ DiscriminationMap here:

/**
 * Course
 *
 * @ORM\Table(name="Course")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="course_type", type="string")
 * @ORM\DiscriminatorMap({"A" = "ClassA", "B" = "ClassB", "C" = "ClassC"})
 */
abstract class Course
{
 //...
}

class ClassA extends Course
{
 //...
}

class ClassB extends Course
{
 //...
}

class ClassC extends Course
{
 //...
}

Or something like that.

Course ClassA, ClassB, ClassC, "extends" .

. , , - .

+4
1

:

use Doctrine\ORM\Mapping as ORM;
    /**
     * @ORM\Entity
     * @ORM\Table(name="class")
     * @ORM\InheritanceType("JOINED")
     * @ORM\DiscriminatorColumn(name="type", type="string", length=20)
     * @ORM\MappedSuperclass
     * @ORM\DiscriminatorMap({
     *     "class_a" = "ClassA",
     *     "class_b" = "ClassB",
     *     "class_c" = "ClassC"
     * })
     */
    abstract class AbstractClass
    {
       ... common fields
        /**
         * @var PersistentCollection
         *
         * @ORM\OneToMany(targetEntity="Course", cascade={"persist"}, mappedBy="course")
         */
        protected $courses;

    }
   /**
    * @Entity
    * @ORM\Table(name="class_a")
    */
    class ClassA extends AbstractClass
    {
     //...
    }
   /**
    * @Entity
    * @ORM\Table(name="class_b")
    */
    class ClassB extends AbstractClass
    {
     //...
    }
   /**
    * @Entity
    * @ORM\Table(name="class_c")
    */
    class ClassC extends AbstractClass
    {
     //...
    }

   /**
    * @Entity
    * @ORM\Table(name="course")
    */
    class Course
    {

        /**
         * @var AbstractClass
         * @ORM\ManyToOne(targetEntity="AbstractClass", inversedBy="course")
         * @ORM\JoinColumn(onDelete="CASCADE")
         */
        private $course;
        ....

    }

, classes course ( , , ).

DQL , INSTANCE OF.

+3

Source: https://habr.com/ru/post/1680563/


All Articles