Forms dropdown load data from field

Hi,
I’m having a few issues trying to figure out the following, even reading the documentation it makes little sense…
I’ve creating the following 2 tables
partners - id - partner_type_id (partner_types id) FK

partner_types - id - partner_type

generated the code using cake bake all Partners
generated the code using cake bake all PartnerTypes

on the add, edit form for the partner the dropdown only displays the ID from the partner_type table how do I get it to display partner_type with value of id??

Thanks

Mal

It sounds like the array that is being provided to the Form-control() to render the partner_type dropdown is in the form:

[ id1 => id1, id2 => id2]

You’ll want it to be

[id1 => type1, id2 => type2]

You haven’t shown any code from your controller, but probably there is a find(‘list’) that is producing this array.

Looking at this should help fix your problem:

https://book.cakephp.org/4/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs

Thanks @dreamingmind that worked
I amended this line in my Controller
$partnerTypes = $this->Partners->PartnerTypes->find('list', ['keyField' => 'id', 'valueField' => 'partner_type']);

so it is now displaying the options correctly, only thing is how do I add <option> Select Option</option> to the start of the drop down??

Regards

Mal

'empty' => 'Select Option' in the array that you pass to the input (or control method). Not the array of choices that you pass in the 'options' key… Like:

echo $this->Html->input('partner_type_id', [
    'options' => $partnerTypes,
    'empty' => 'Select Option',
]);

Thanks @Zuluru

I’m now struggling with another bit of code :frowning: which I can’t get my head around how to write so it doesn’t throw an error…
I have several tables using the id from the partner table as a FK I’ve managed to get slug working, but now I have done this the data isn’t displaying on the view page for the other table data how do I write the following to get them to display??

public function view($slug = null)
    {
        $partner = $this->Partners->findBySlug($slug)->firstOrFail(
		->get($id [
            'contain' => ['PartnerTypes', 'SubscriptionTypes', 'Users', 'PartnerFacilities', 'PartnerPegs', 'PartnerWatersSpecies', 'PartnersWaters'],
        ]));
		$this->viewBuilder()->setLayout('partners');
        $this->set('partner', $partner);
    }

Regards

Mal

  1. For a new problem, you should make a new post.
  2. If you’re getting error messages, you should tell us what the error is.
  3. If the problem is in generating output, you should show the code that generates the output.

thanks @Zuluru will do