Hello community! i’m new in cakePHP 3.x and i’m trying to find some help here 'cause i’m stuck with this.
I have 2 tables with their respective models.
Table 1: users Model name: UsersTable
Table 2: contact_types Model name: ContactTypesTable
I have also a third table: users_contact_types with it model UsersConctactTypesTable
This table i used to make the relation between users and their contact types. The fields of that table are: user_id, contact_type_id and value.
For example:
Supose we have a user named John with ID = 1 and supose we have 3 different contact types (telephone,email and skype…with ID 1,2 and 3 respectively)
Aim to achieve:
Store in the relation table (users_contact_types) something like this:
user_id contact_type_id value
1 2 agasiadolfo@gmail.com
1 3 agasi.adolfo
…
and so on.
This relation (according to the documentation Associations - Linking Tables Together - 3.10 ) is a belongs-to-many relation.
I followed all the steps to create the models, tables, and tempted to save the data in the controller but still having some issues to make this work.
I also read in the documentation(Saving Data - 3.10) that to save some extra fields we have to use _joinData but i don’t understand how to make it work.
Here i copy my files so you can have a look and tell me what you think.
INITIALIZE METHOD OF USERSTABLE MODEL:
public function initialize(array $config) {
parent::initialize($config);
$this->table('users'); $this->displayField('id'); $this->primaryKey('id');
$this->addBehavior('Timestamp'); $this->addBehavior('Acl.Acl', ['type' => 'requester']);
$this->belongsToMany('ContactTypes', [ 'through' => 'UsersContactTypes', ]);
}
INITIALIZE METHOD OF CONTACTTYPES MODEL
public function initialize(array $config) {
parent::initialize($config);
$this->table('contact_types'); $this->displayField('name'); $this->primaryKey('id');
$this->belongsToMany('Users', [ 'through' => 'UsersContactTypes', ]); }
INITIALIZE METHOD OF USERSCONTACTTYPES MODEL
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users_contact_types'); $this->displayField('user_id'); $this->primaryKey(['user_id', 'contact_type_id']);
$this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER', 'className' => 'UsersCrudAuth.Users' ]); $this->belongsTo('ContactTypes', [ 'foreignKey' => 'contact_type_id', 'joinType' => 'INNER', 'className' => 'UsersCrudAuth.ContactTypes' ]); }
THIS IS MY Users/add.ctp
- <?= __('Actions') ?>
- <?= $this->Html->link(__('List Users'), ['action' => 'index']) ?>
- <?= $this->Html->link(__('List Aro'), ['controller' => 'Aros', 'action' => 'index']) ?>
- <?= $this->Html->link(__('New Aro'), ['controller' => 'Aros', 'action' => 'add']) ?>
- <?= $this->Html->link(__('List Contact Types'), ['controller' => 'ContactTypes', 'action' => 'index']) ?>
- <?= $this->Html->link(__('New Contact Type'), ['controller' => 'ContactTypes', 'action' => 'add']) ?>
<?= $this->Form->create($user) ?> <?= __('Add User') ?> <?php echo $this->Form->input('username'); echo $this->Form->input('name'); echo $this->Form->input('surname'); echo $this->Form->input('gender'); echo $this->Form->input('password'); echo $this->Form->input('group_id', ['options' => $groupsList]); echo $this->Form->input('status');
/** For now i render the fields like this, but then i'm going to use a table with a 'add register' button that creates a new node to the table incrementing the id */
echo $this->Form->input(‘contact_types.0._id’, [‘options’ => $contactTypes]);
echo $this->Form->input(“contact_types.0._joinData.value”);
echo $this->Form->input(‘contact_types.1._id’, [‘options’ => $contactTypes]);
echo $this->Form->input(“contact_types.1._joinData.value”);> ?>
</fieldset> <?= $this->Form->button(__('Submit')) ?> <?= $this->Form->end() ?>
And my UsersController.php (only the add method for now)
public function add() {
$user = $this->Users->newEntity();
if ($this->request->is(‘post’)) {
$user = $this->Users->patchEntity($user, $this->request->data);
/** This is the posted data from the form */ debug($this->request->data);
/** Here i tempt to save the user model and the associated model whith it _joinData field */ debug($this->Users->save($user, ['associated' => ['ContactTypes','ContactTypes._joinData']] )); die();
if ($this->Users->save($user)) { $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.')); } }
$groupsTable = TableRegistry::get('Groups'); $groupsList = $groupsTable->find('list', ['limit' => 200]);
$contactTypes = TableRegistry::get('ContactTypes')->find('list', ['limit' => 200]); $this->set(compact('user', 'contactTypes','groupsList')); $this->set('_serialize', ['user']); }
Also i add here the debug of the request->data sended from the form.
> /plugins/UsersCrudAuth/src/Controller/UsersController.php (line 105)
> [
> 'username' => 'john',
> 'name' => '',
> 'surname' => '',
> 'gender' => 'm',
> 'password' => 'john',
> 'group_id' => '2',
> 'status' => '',
> 'contact_types' => [
> (int) 0 => [
> '_id' => '5',
> '_joinData' => [
> 'value' => 'john@doe.com'
> ]
> ],
> (int) 1 => [
> '_id' => '4',
> '_joinData' => [
> 'value' => 'john.doe'
> ]
> ]
> ]
> ]
> /plugins/UsersCrudAuth/src/Controller/UsersController.php (line 110)
> false
Well, i hope you can help me with this because i can’t figure out whats going on here.
By the way, thanks for cooperating.