加入symfony 2

时间:2017-04-07 07:26:22

标签: mysql symfony join doctrine-orm symfony-2.8

我有3个表,我使用YAML,它们是这样连接的:

培训:

oneToMany:
        exerciseTrainings:
            targetEntity: ExerciseTraining
            mappedBy: training
            cascade: ["persist", "merge"]
            joinColumn:
                name: id
                referencedColumnName: training_id

锻炼:

oneToMany:
    exerciseTrainings:
        targetEntity: ExerciseTraining
        mappedBy: exercise
        cascade: ["persist", "merge"]
        joinColumn:
            name: id
            referencedColumnName: exercise_id

trainingExercises:

manyToOne:
    exercise:
        targetEntity: Exercise
        inversedBy: exerciseTrainings
        joinColumn:
            name: exercise_id
            referencedColumnName: id
    training:
        targetEntity: Training
        inversedBy: exerciseTrainings
        joinColumn:
            name: training_id
            referencedColumnName: id

exerciseTraining table:

我需要"锻炼姓名"从表练习(基本上来自exercise_translation的名字,但我认为它不是一个问题)与我的训练相关,我也需要"设置"来自exerciseTrainings表。所以我接受了培训并尝试加入表格:

$training = $em->getRepository('TrenkaTrainingBundle:Training')->findOneByAlias($alias);

$exercises = $em
    ->getRepository('TrenkaTrainingBundle:Exercise')
    ->createQueryBuilder('e')
    ->innerJoin('e.exerciseTrainings', 't')
    ->where('t.training_id = ' . $training->getId())
    ->getQuery()
    ->getResult();

但是我收到了这个错误:

  

Class TrainingBundle \ Entity \ ExerciseTraining没有字段或   名为training_id的协会

也许某人有相同的结构并知道如何使其发挥作用?另外如何在TWIG中的2-3个表中显示值?

1 个答案:

答案 0 :(得分:1)

您的 ExerciseTraining 实体中没有 training_id 字段,因此您无法在 ExerciseTraining 的where子句中使用它您拥有 training 字段的实体,以便您可以尝试以下内容:

$exercises = $em
    ->getRepository('TrenkaTrainingBundle:Exercise')
    ->createQueryBuilder('e')
    ->innerJoin('e.exerciseTrainings', 'et')
    ->where('et.training = :training')
    ->setParameter('training', $training)
    ->getQuery()
    ->getResult();