动态地将数据库选择添加到选择类型

时间:2019-04-08 16:35:39

标签: php database symfony doctrine-orm

这是我的主意,因此更容易理解。

我正在制作一个问答类型,因此我可以练习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)

如您所见,一张桌子上所有的问题和一个答案都与另一个相连。

现在是我的问题。

我希望它也显示在Fronend中。

基本上我想做到这一点。哪个有效(但不适用于我的答案/有效)

 $dummyArray = [
                'answer 1' => 1,
                'answer 2' => 0,
                'answer 3' => 0
            ];

        $builder
            //->add('question', TextType::class)
            ->add(
                'Answers',
                ChoiceType::class,
                [
                    'choices' => $dummyArray,
                    'expanded' => true
                ]
            )

我尝试了遍历im给```'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

谢谢大家花时间弄清楚我的烂摊子。

0 个答案:

没有答案
相关问题