C5-8.4* Doctrine how to get associated list in array of strings format
Permalink
I'm struggling to get a list of table values to display properly in a select element. I get not only a dropdown box of strings, but each value has also got their indexes shown in the dropdown box.
Here's the method of a CarCategory class to get values:
in the controller:
in the view:
But what I get shown is:
0
Budget
1
Economy
etc.
And
does the same.
Here's the method of a CarCategory class to get values:
public function getCategoryList() { $em = \ORM::entityManager(); $query = $em->createQuery('SELECT c.category FROM Car\CarCategory c'); return $query->getResult(); }
in the controller:
$categoryList = CarCategory::getCategoryList(); $categories = array(); if (is_array($categoryList) && count($categoryList) > 0) { foreach ($categoryList as $key => $value) { array_push($categories, $value); } } $this->set("categories", $categories);
in the view:
echo $form->select('category', $categories, $selected_category);
But what I get shown is:
0
Budget
1
Economy
etc.
And
$this->set("categories", array_values($categoryList));
does the same.
Well, now it shows
This is the first time ever I see such behavior of the select box. I always passed arrays to it and it simply showed the values in the dropdown, regardless if the array was simple or associative.
Yes, I just want values shown in the dropdown.
Array Economy
This is the first time ever I see such behavior of the select box. I always passed arrays to it and it simply showed the values in the dropdown, regardless if the array was simple or associative.
Yes, I just want values shown in the dropdown.
http://sandbox.onlinephpfunctions.com/code/0a003939e78cbd053758cbe06f8b85cc2e18d717
But I don't know what's exactly in your $value variable.
But I don't know what's exactly in your $value variable.
I got it! I haven't realized it was a 2D array. So this worked:
foreach ($categoryList as $key) { array_push($categories, $key['category']); }
Yes. Hence my comment about `$value`. Glad you sorted it out.
You'd do