继承和关联方案

时间:2016-05-30 12:50:38

标签: php mysql symfony doctrine-orm doctrine

我在Symfony2中有一个工作实现,其中包含以下模型的学说。 enter image description here

  • 家长可以申请一项/多项培训
  • 培训基于特定技能,但您可以在不同日期为同一技能进行多次培训。
  • 一旦家长参加培训,他就可以被标记为“合格”以获得与培训相关的技能并成为培训生。
  • 家长可以参加同一项技能的多项培训,但只能为给定的技能标记“合格”一次
  • 学员可以在许多不同的技能上“合格”技能

父母和受训者之间的继承和关联(一对一)已使用单表继承实现,如下所示:

Parents\ParentsBundle\Entity\Parents:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorColumn:
    name: type
    type: string
discriminatorMap:
     parents: Parents
     trainee: Parents\TraineeBundle\Entity\Trainee
table: Parents
repositoryClass: Parents\ParentsBundle\Repository\ParentsRepository
id:
    id:
        type: integer
        generator:
            strategy: AUTO
fields:
    firstname:
        type: string
        length: 250
    lastname:
        type: string
        length: 250
    dob:
        type: date
        nullable: true
lifecycleCallbacks:
    prePersist: [ setDateDeCreationValue ]
manyToMany:
    trainings:
        targetEntity: Training\TrainingBundle\Entity\Training
        mappedBy: parents
        orphanRemoval: true
        cascade: ["all"]
oneToOne:
    trainee:
        targetEntity: Parents\TraineeBundle\Entity\Trainee
        inversedBy: parents
        cascade:  ["all"]
        joinColumns:
            trainee_id:
                referencedColumnName: id
indexes:
    nom_prenoms_idx:
        columns: [ firstname, lastname ]


Parents\TraineeBundle\Entity\Trainee:
    type: entity
    extends: Parents\ParentsBundle\Entity\Parents
    repositoryClass: Parents\TraineeBundle\Repository\TraineeRepository
    manyToMany:
            skills:
                targetEntity: Training\SkillBundle\Entity\Skill
                mappedBy: trainees
    oneToOne:
            parents:
                targetEntity: Parents\ParentsBundle\Entity\Parents
                mappedBy: trainee

Trianing\SkillBundle\Entity\Skill:
    type: entity
    table: Skill
    repositoryClass: Training\SkillBundle\Repository\SkillRepository
    id:
        id:
            type: integer
            generator:
                strategy: auto
    fields:
        title:
            type: string
            length: 80
            unique: true
        description:
            type: string
            length: 250
            nullable: true
    manyToMany:
        trainees:
            targetEntity: Parents\TraineeBundle\Entity\Trainee
            inversedBy: skills
            cascade: ["all"]
            joinTable:
                name: trainess_skills
                joinColumns:
                    skill_id:
                        referencedColumnName: id
                        nullable: false
                        onDelete: CASCADE
                inverseJoinColumns:
                    trainee_id:
                        referencedColumnName: id
                        nullable: false
    uniqueConstraints:
        titre_UNIQUE:
            columns:
                - titre

但是,我希望从“父母”列表中排除“培训”组的技能“合格”:

  1. 该技能上的所有准备好的实习生
  2. 我确实有以下SQL查询,它使我能够获得预期的结果,但由于协会链接到实体,我无法在Doctrine中运行。 SQL查询

        SELECT p.*
      FROM Parents p
      LEFT JOIN training_formations tf
        ON p.id = tf.parents_id
      LEFT JOIN Training t
        ON tf.training_id = t.id
      LEFT JOIN Parents trainee
        ON p.intervenant_id = trainee.id
      LEFT JOIN trainees_skills ts
        ON trainee.id = ts.trainee_id
    WHERE t.id=@trainingId and (t.skill_id <> ts.skill_id or p.trainee_id is null);
    

    The Doctrine Query:

    $qb = $this->createQueryBuilder('p');
            $qb->select('p')
                ->leftJoin('p.trainings', 't')
                ->leftJoin('p.trainee','tr')
                ->leftJoin('tr.skill','s')
                ->where('t.id = :trainingId')
                ->andWhere($qb->expr()->orX(
                        $qb->expr()->neq('t.skill','tr.skill'),
                        $qb->expr()->isNull('p.trainee')
                                            )
                        )
                ->setParameter('trainingId', $trainingId)
                ->orderBy('p.firstname', 'ASC');
            return $qb;
    

    结果查询抛出一个PathExpression错误,我试图通过在外键上使用'IDENTITY()'方法来纠正但是ut不起作用。

    我是否遗漏了某些东西或者实施了什么?

1 个答案:

答案 0 :(得分:0)

在更新的Doctrine Query下面找到解决方案:

$qb = $this->createQueryBuilder('p');
    $qb->select('p')
        ->leftJoin('p.trainings', 't')
        ->leftJoin('p.trainee','tr')
        ->leftJoin('tr.skill','s')
        ->where('t.id = :trainingId')
        ->andWhere($qb->expr()->orX(
                $qb->expr()->neq('t.skill','s.id'),
                $qb->expr()->isNull('s.id')
                                    )
                )
        ->setParameter('trainingId', $trainingId)
        ->orderBy('p.firstname', 'ASC');
    return $qb;

但是,从各种阅读来看,组合似乎比这里使用的继承更适合。但这是另一个话题。