duanqian9593 2016-05-30 12:50
浏览 45
已采纳

继承和关联方案中的Doctrine Query问题

I've a working implementation in Symfony2 with doctrine of the following model. enter image description here

  • A Parent can apply for one/more Training(s)
  • A Training is based on a specific Skill, but you can have multiple trainings at different date for the same skill.
  • Once a Parent attends the training he can be marked as 'qualified' for the Skill related to the Training taken and becomes a Trainee.
  • A Parent can attend multiple Training on the same Skill but will only marked 'qualified' only once for the given Skill
  • The Trainee can be 'qualified' on many differents Skills

The Inheritance and Association (one to one) between Parents and Trainee has been implemented using a single table inheritance as seen below:

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

However, im looking to exclude from the list of Parents to be 'qualified' on a Skill from a Training group those who are:

  1. All ready Trainee on that Skill

I do have the following SQL Query which enable me to get the expected result, but i'm unable to have a working one in Doctrine since the associations links to Entities. SQL Query

    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;

The resulting query throws a PathExpression error, which i tempted to correct by using the 'IDENTITY()' method on the foreign keys but ut did not work.

Did i miss something or worongly implemented something?

  • 写回答

1条回答 默认 最新

  • dpz90118 2016-05-31 14:44
    关注

    Found the solution,below the updated 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;
    

    However, from various reading it seems that a Composition will be better suited than the Inheritance used here. But that's another topic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀