Hello Community, i’m here to ask for help with the Form Helper.
Context:
- I have 3 associated models (UsersTable, ContactTypesTable, and UsersContactTypes)
- The add method is working perfect. The data is being stored in the DB with the associated model information.
- The edit.ctp is showing the data of the user perfect too. Also is showing the data of the associated model, but only is being displayed the value field.
- Also the DEBUG is showing the data of the user and the associated model perfectly. So the view has all the information it needs to do it work.
The PROBLEM:
- The DROPDOWN i use to show the different ‘contact types’ is being displayed well, but it’s missing the ‘selected’ attribute. This make the dropdown not show the correct value that is stored in the DB.
I tried all day make this work but i’m failing. Need some ideas.
UsersTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
/** Acl plugin behavior. */
$this->addBehavior('Acl.Acl', ['type' => 'requester']);
/** Set the association between models. */
$this->hasMany('UsersContactTypes', [
'dependent' => true
]);
}
ContactTypesTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->table('contact_types');
$this->displayField('name');
$this->primaryKey('id');
/** Set the association between models. */
$this->hasMany('UsersContactTypes',[
'dependent' => true
]);
}
UsersContactTypesTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users_contact_types');
$this->displayField('user_id');
$this->primaryKey(['user_id', 'contact_type_id']);
/** Set the association between models. */
$this->belongsTo('Users');
$this->belongsTo('ContactTypes');
}
edit.ctp
//First pair of contact type => value
echo $this->Form->input('users_contact_types[0].contact_type_id');
echo $this->Form->input('users_contact_types.0.value');
//Second pair of contact type => value
echo $this->Form->input('users_contact_types[1].contact_type_id');
echo $this->Form->input('users_contact_types.1.value');
UsersController.php
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['UsersContactTypes']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
/** Save the User model with it associated model UsersContactTypes. */
if ($this->Users->save($user,['associated' => ['UsersContactTypes']])) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
/** Prepare variables that i use in the view. */
$groups = TableRegistry::get('Groups')->find('list', ['limit' => 200]);
$contactTypes = TableRegistry::get('ContactTypes')->find('list', ['limit' => 200])->all();
/** Set the data so it becames available in the view. */
$this->set(compact('user', 'contactTypes','groups'));
$this->set('_serialize', ['user']);
}
Debug of the $user in the edit.ctp file:
/plugins/UsersCrudAuth/src/Template/Users/edit.ctp (line 35)
object(UsersCrudAuth\Model\Entity\User) {
'id' => (int) 1,
'username' => 'aagasi',
'name' => 'Adolfo',
'surname' => 'Agasi',
'gender' => 'Masculino',
'password' => '$2y$10$1/syLa4VuX6inYyKPF7MZOlTilYO5B/aunxnyGE1wALJvq4nau6xa',
'group_id' => (int) 1,
'created' => object(Cake\I18n\FrozenTime) {
'time' => '2016-05-09T18:30:42+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\FrozenTime) {
'time' => '2016-05-16T01:20:56+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'status' => 'Active',
'users_contact_types' => [
(int) 0 => object(Cake\ORM\Entity) {
'user_id' => (int) 1,
'contact_type_id' => (int) 3,
'value' => 'agasi.adolfo',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'UsersContactTypes'
},
(int) 1 => object(Cake\ORM\Entity) {
'user_id' => (int) 1,
'contact_type_id' => (int) 5,
'value' => 'agasiadolfo@gmail.com',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'UsersContactTypes'
}
],
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'UsersCrudAuth.Users'
}
Snapshot of user edit form.
Hope you can help me figure out whats going on with my code.
Regards.