Вот моя идея, чтобы ее было легче понять.
Я делаю Тип вопроса и ответа От, чтобы я мог практиковать Symfony и php.
Идея есть.
У вас есть 1 вопрос.
И несколько возможных ответов, которые связаны с вопросом.
Надеюсь, мои Select * ALL
прояснят это.
mysql> SELECT * FROM question;
+----+--------------------+
| id | question |
+----+--------------------+
| 1 | This is question 1 |
| 2 | This is question 2 |
| 3 | This is question 3 |
+----+--------------------+
3 rows in set (0,00 sec)
mysql> SELECT * FROM answer;
+----+-------------------------+-------------+-------+
| id | answer | question_id | valid |
+----+-------------------------+-------------+-------+
| 6 | Answer from DB: True | 1 | 1 |
| 7 | Answer 2 from DB: False | 1 | 0 |
| 8 | Answer 2 from DB: False | 1 | 0 |
| 10 | Answer 3: true | 2 | 1 |
| 11 | Answer 3: false | 2 | 0 |
| 12 | Answer 3: false | 2 | 0 |
+----+-------------------------+-------------+-------+
6 rows in set (0,00 sec)
Как видите, в 1 таблице есть все вопросы и один ответ на все вопросы, связанные с другим.
Теперь к моей проблеме.
Я хочу, чтобы это также отображалось в Fronend.
В основном, я хочу добиться этого .. Что работает (но не с моими ответами / Действителен)
$dummyArray = [
'answer 1' => 1,
'answer 2' => 0,
'answer 3' => 0
];
$builder
//->add('question', TextType::class)
->add(
'Answers',
ChoiceType::class,
[
'choices' => $dummyArray,
'expanded' => true
]
)
Я пробовал foreach над массивом, который я даю `` `'choices'````` *
$dummyArray = [];
foreach ($answers as $answer) {
$dummyArray = [$answer->getAnswer() => $answer->getValid()];
}
Но это показывает только один ответ. Который будет 12 | Answer 3: false
Вот моя моя свалка от: $answers = $this->answerRepository->findAll();
rray (size=6)
0 =>
object(App\Entity\Answer)[351]
private 'id' => int 6
private 'answer' => string 'Answer from DB: True' (length=20)
private 'question' =>
object(App\Entity\Question)[564]
private 'id' => int 1
private 'question' => string 'This is question 1' (length=18)
private 'answers' =>
object(Doctrine\ORM\PersistentCollection)[562]
...
private 'valid' => boolean true
1 =>
object(App\Entity\Answer)[255]
private 'id' => int 7
private 'answer' => string 'Answer 2 from DB: False' (length=23)
private 'question' =>
object(App\Entity\Question)[564]
private 'id' => int 1
private 'question' => string 'This is question 1' (length=18)
private 'answers' =>
object(Doctrine\ORM\PersistentCollection)[562]
...
private 'valid' => boolean false
2 =>
object(App\Entity\Answer)[354]
private 'id' => int 8
private 'answer' => string 'Answer 2 from DB: False' (length=23)
private 'question' =>
object(App\Entity\Question)[564]
private 'id' => int 1
private 'question' => string 'This is question 1' (length=18)
private 'answers' =>
object(Doctrine\ORM\PersistentCollection)[562]
...
private 'valid' => boolean false
3 =>
object(App\Entity\Answer)[609]
private 'id' => int 10
private 'answer' => string 'Answer 3: true' (length=14)
private 'question' =>
object(App\Entity\Question)[544]
private 'id' => int 2
private 'question' => string 'This is question 2' (length=18)
private 'answers' =>
object(Doctrine\ORM\PersistentCollection)[293]
...
private 'valid' => boolean true
4 =>
object(App\Entity\Answer)[608]
private 'id' => int 11
private 'answer' => string 'Answer 3: false' (length=15)
private 'question' =>
object(App\Entity\Question)[544]
private 'id' => int 2
private 'question' => string 'This is question 2' (length=18)
private 'answers' =>
object(Doctrine\ORM\PersistentCollection)[293]
...
private 'valid' => boolean false
5 =>
object(App\Entity\Answer)[607]
private 'id' => int 12
private 'answer' => string 'Answer 3: false' (length=15)
private 'question' =>
object(App\Entity\Question)[544]
private 'id' => int 2
private 'question' => string 'This is question 2' (length=18)
private 'answers' =>
object(Doctrine\ORM\PersistentCollection)[293]
...
private 'valid' => boolean false
Спасибо всем, что нашли время, чтобы выяснить мой беспорядок.