简体   繁体   English

与学说Symfony的条件关系

[英]Conditional relations with doctrine Symfony

I have need to create this database scheme in Symfony Entities : 我需要在Symfony实体中创建这个数据库方案:

ClassA: ClassA的:

  • id ID
  • name 名称
  • ... some other attributes only for class A ......仅针对A类的一些其他属性

ClassB: ClassB的:

  • id ID
  • name 名称
  • ... some other attributes only for class B ......仅适用于B类的其他一些属性

ClassC: ClassC:

  • id ID
  • name 名称
  • ... some other attributes only for class C ......仅适用于C类的其他一些属性

Course: 课程:

  • id ID
  • name 名称
  • type 类型
  • class_id 班级号
  • class_type [A, B, C] class_type [A,B,C]
  • ... ...

I need to create relations between Course Entity (using the class_id) with the other entities (ClassA, ClassB, ClassC) (using the id) and using the class_type (A, B, C). 我需要在Course Entity(使用class_id)和其他实体(ClassA,ClassB,ClassC)(使用id)和使用class_type(A,B,C)之间创建关系。

A Course is not a class and a class is not a course. 课程不是课程,课程不是课程。 There is no inheritance here. 这里没有继承权。 In my project I'm using this concept (id and type to map different entities). 在我的项目中,我正在使用这个概念(id和类型来映射不同的实体)。 So I need to solve this problem with a simple example like this one 所以我需要用这样一个简单的例子来解决这个问题

I thought about using @ORM\\DiscriminationMap here: 我想过在这里使用@ORM \\ DiscriminationMap:

/**
 * 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 this. 或类似的东西。

But I don't want to copy the Course's attributes in the ClassA, ClassB, ClassC entities because "extends" will copy the parent entity's attributes to the child. 但我不想在ClassA,ClassB,ClassC实体中复制Course的属性,因为“extends”会将父实体的属性复制到子级。

It's not about inheritence. 这不是关于遗产。 I need to keep the database scheme described above without adding any additional attributes from the other entities. 我需要保留上述数据库方案,而不添加其他实体的任何其他属性。

Thank you 谢谢

Solution might be next: 解决方案可能是下一个

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;
        ....

    }

So now you'll have single table for all your classes with one to many relation to course (this is just an example, you could use one to many or many to many, depends on your needs). 所以现在你的所有classes都有一个单独的表格,其中一对多关系course (这只是一个例子,你可以使用一对多或多对多,取决于你的需要)。

Within DQL you can find out which class is used with INSTANCE OF . 在DQL中,您可以找到与INSTANCE OF一起使用的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM