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:
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.

 
A3020 replied on at Permalink Reply
A3020
So you don't want the indexes, but values instead?

You'd do

$this->set('categories', array_combine($categories, $categories));
alboraus replied on at Permalink Reply
Well, now it shows
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.
A3020 replied on at Permalink Reply
A3020
http://sandbox.onlinephpfunctions.com/code/0a003939e78cbd053758cbe06f8b85cc2e18d717

But I don't know what's exactly in your $value variable.
alboraus replied on at Permalink Reply
I assume $value is a value of the category returned by the Doctrine query

Here's a var_dump of the query result:
array(3) { [0]=> array(1) { ["category"]=> string(6) "Budget" } [1]=> array(1) { ["category"]=> string(7) "Economy" } [2]=> array(1) { ["category"]=> string(6) "Luxury" } }
alboraus replied on at Permalink Reply
I got it! I haven't realized it was a 2D array. So this worked:
foreach ($categoryList as $key) {
    array_push($categories, $key['category']);
}
A3020 replied on at Permalink Reply
A3020
Yes. Hence my comment about `$value`. Glad you sorted it out.