2017-04-07 66 views
0

我有3個表和我使用YAML,這是相互連接是這樣的:加入symfony的2

培訓:

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

也許有人有相同的結構,並知道如何使它發揮作用?另外如何在TWIG中提供2-3個表格的值?

回答

1

你沒有training_id場在ExerciseTraining實體,所以你不能你的where子句中使用它,在你ExerciseTraining實體必須培訓場,所以你可以嘗試這樣的事情:

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

感謝的答案,但它得到 錯誤:字符串應爲結束,得到了「」 – Stopper

+0

哇!感謝它的工作。我得到的樹枝名{%用於鍛鍊鍛鍊%} {{exercise.name}}但是,如何從樹枝exerciseTraining拿到套? – Stopper

+1

什麼'{{鍛鍊。 exerciseTrainings}}'? – Shady