has and belongs to many - How to show associated data in add form in cakephp 3.x -


i have 2 tables

create table `user_roles` (    `id` int(11) not null auto_increment,    `role_name` varchar(20) default null,     primary key (`id`) ) engine=innodb auto_increment=39 default charset=latin1  create table `users` (    `id` int(11) not null auto_increment,    `username` varchar(20) default null,    `email` varchar(255) default null,    `password` varchar(255) default null,    `user_role_id` int(11) default null,    `status` int(11) default null,     primary key (`id`) ) engine=innodb default charset=latin1 

i created insertion , deletion user roles . no have add users user roles. in user adding form user role displaying empty. here code: usertable.php: namespace app\model\table;

            use cake\orm\table;         use cake\validation\validator;          class userstable extends table         {             public function initialize(array $config)            {               $this->hasmany('userroles', [                     'foreignkey' => 'id',                     'sort' => ['userroles.role_name', 'asc']                 ]);                 $this->displayfield('role_name');         }   }          usercontroller.php          public function add()         {             $this->set('title', 'add users');             $users = $this->users->newentity();             if($this->request->is('post')):                 $users = $this->users->patchentity($user, $this->request-                   >data);                 if($this->users->save($user)):                     $this->flash->success(__('user created                       successfully.'));                     return $this->redirect(['action' => 'index']);                 endif;             endif;             $this->set('users', $this->users);             $this->set('user_roles', $this->users->userroles->find('list',               ['id', 'role_name']));         }             **add.ctp**         <?php          //print_r($user_roles);         echo $this->form->create($users, ['class' => 'form']);             echo $this->form->input('username');         echo $this->form->input('email');         echo $this->form->input('user_role_id');         echo $this->form->input('password');         echo $this->form->button(__("<i class='fa fa-save'></i> save"));         echo $this->form->end();         ?>         **userrolestable.php**         namespace app\model\table;          use cake\orm\table;         use cake\validation\validator;          class userrolestable extends table         {         public function initialize(array $config)         {         $this->belongstomany('users', [                 'foreignkey' => 'user_role_id'             ]);         }          public function validationdefault(validator $validator)         {         $validator->notempty('role_name', 'role name cannot empty.');         return $validator;         }} 

its showing empty select box user_role_id. please me.

you need camelcase view variable options magic happen, should $userroles rather $user_roles:-

$this->set('userroles', $this->users->userroles->find('list', ['id', 'role_name'])); 

cake know associate variable form field. otherwise have specify variable used options suggested jun.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -