加入 symfony 2

Joins symfony 2

我有 3 个 table,我使用 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

训练练习:

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

我需要 "exercise name" 来自 table 与我的培训相关的练习(基本上名称来自 exercise_translation,但我认为这不是问题)并且我还需要 "sets" 来自 exerciseTrainings table。所以我接受了培训并尝试加入 tables:

$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 has no field or association named training_id

也许有人有相同的结构并且知道如何让它发挥作用?另外如何在 TWIG 中显示 2-3 tables 的值?

您的 ExerciseTraining 实体中没有 training_id 字段,因此您不能在您的位置使用它子句,在你的 ExerciseTraining 实体中你有 training 字段,所以你可以尝试这样的事情:

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