php - cakephp 3.0 how to populate a select field with values instead of id -


i looking previous answer, ones i've found related older cakephp versions

i have 2 tables, 'magazines' , 'issues' there relation 'issues' belongsto 'magazines', issuestable looks like:

public function initialize(array $config){  $this->belongsto('magazines', [     'foreignkey' => 'id' ]); } 

table magazines has 2 fields, magazines.id , magazines.name

table issues has 2 fields, issues.id, issues.magazine_id issues.magazine_id foreign key

to populate select input in issues view magazine.name values , save issues.magazine_id, i've set controller this

$this->set('magazines', $this->issue->magazine->find('list')); 

then i've added following code issue view add.cpt

    <?php     echo $this->form->input('name', [     'type' => 'select',     'multiple' => false,     'options' => $magazines,      'empty' => true]);     ?> 

but input select issues.magazine_id values instead of magazines.name

thanks , comments

you want use find('list') return primary key , display field:-

$this->set(     'magazines',      $this->issues->magazines->find('list') ); 

then in form need input name magazine_id if you're wanting set foreign key association:-

echo $this->form->input(     'magazine_id',      [         'type' => 'select',         'multiple' => false,         'options' => $magazines,          'empty' => true     ] ); 

see docs more info.

update

if you're experiencing issues find('list') perhaps because model's displayfield not getting set correctly. cake determines displayfield model on initialisation. if isn't working, or want different field can set manually in model's initialize() method. e.g.:-

class magazinestable extends table {      public function initialize(array $config)     {         $this->displayfield('name');     } } 

changing 'name' appropriate field.

alternatively, can choose field cake use values returned find('list') (this particularly useful when want override default displayfield). e.g.:-

$this->issues->magazines->find('list', [     'keyfield' => 'id',     'valuefield' => 'name' ]); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -