Output is in json or something


#1

I have this in my controller:

    $list = $this->Categories->find('children', ['for' => 7, 'fields' => 'name']);
            
            $this->set(compact('categories', 'list'));
            $this->set('_serialize', ['categories']);

and this in my view:

echo $this->Form->control('parent_id', ['options' => $list]); ?>

For some reason, the displayed text is JSON like this:

{ "name": "Billy" }

I have no idea what I am doing wrong, all my other stuff outputs normal.


#2

You have to call toArray at the end of the chain


#3

If I change to this, it still doesn’t fix it.

$query = $this->Categories->find('children', ['for' => 7, 'fields' => 'name']);
        $list = $query->toArray();

#4

instead of field name, you should use the list finder, and then to array


#5

I don’t understand or something, because I try this and it gives me the whole row in JSON.

$list = $this->Categories->find('children', ['for' => 7, 'finder' => 'name'])->toArray();

Do you mean create my own finder method?


#6

Try something like this:

$list = $this->Categories->find('list', ['keyField' => 'id', 'valueField' => 'name'])->toArray();

#7

That works, but if I change it from list to children, it goes back to being a json response with the full row.


#8

You can chain it

$list = $this->Categories
    ->find('children', ['for' => 7])
    ->find('list')
    ->toArray();

#9

Thank you, that did it. Sorry I’m new to this, big learning curve for me.