0

Please bear in my mind this isn't originally my code so it's not crystal clear to me and help is extremely welcome as I'm lost.

I want to display rows from another table, but I'm facing some issues. When I use

in controller :

$companies = $this->Users->Companies->find('list');

in view

echo $this->Form->input('companies._ids',['options' =>$companies,'multiple'=>'checkbox']);?>

I get all of my Company's table ids and names in these basic checkboxes.

However, as I want to use every rows and not just id and name, I try using :

$companies = $this->Users->Companies->find('all');

but this triggers two problems : my datas are displayed in arrays like this :

{ "id": 2, "name": "Smith", "city": "Duisburg"}

and second problem : it skips the first row ! I have something with id 1, not showing when using find('all'), but showing when using find('list')

So how can I get every values and display them the way I want to ?

1
  • 1
    I haven't had the problem with missing the first row (yet..). But yes I find that changing the 'find' options often sends back a different array which needs to be parsed differently. It's.. cake :-). Retrieve the 'all' data in a different array and use it separately? It's not worth banging your head on it.. Commented Jun 16, 2017 at 17:39

1 Answer 1

1

If you just want different fields from the list you can specify that.

Specify Fields for Find List

$query = $this->Users->Companies->find('list', [
    'keyField' => 'slug',
    'valueField' => 'title'
]);
$data = $query->toArray();

// Data now looks like
$data = [
    'first-post' => 'First post',
    'second-article-i-wrote' => 'Second article I wrote',
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.