Problem while tempting to save related data

The $this->request->data is as expected. But the model definition isn’t. The idea is to get rid of Users <-> ContactTypes association, doing that the join table becomes a real entity. Check below:

UsersTable.php
$this->hasMany(‘Contacts’);

ContactsTable.php
$this->belongsTo(‘Users’);
$this->belongsTo(‘ContactTypes’);

ContactTypesTable.php
$this->hasMany(‘Contacts’);

Excellent !!

(Sorry for the delay, but the i had to wait 5 hours until the system allow me to post again).

Thanks to your answer i finished this task. I also made some little adjustments so i can keep my naming conventions.

Here i put the updated code so others can use it to have an idea.

Thank you for the patience all of you had with me.


Model 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');
    }

Model ContactTypes.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');
    }

Model UsersContactTypes.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');
    }

Controller UsersController.php

public function add() {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->data);

            /** Save the User model with it associated model UsersContactTypes. */
            $this->Users->save($user,
                ['associated' => ['UsersContactTypes']]
            );

            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.'));
            }
        }

        /** 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']);
    }

View add.ctp

            /** First pair of key => value data. */
            echo $this->Form->input('users_contact_types[0].contact_type_id');
            echo $this->Form->input('users_contact_types[0].value');

            /** Second pair of key => value data. */
            echo $this->Form->input('users_contact_types[1].contact_type_id');
            echo $this->Form->input('users_contact_types[1].value');

And that’s all for now. I’m glad to see that the community is here to help some new cakePHP developers like me. :slight_smile:

Regards.

1 Like

Maybe it is a stupid question but…there is any way to set similar “backlight fade in/out” effect on buttons on web page based on Cake? Something similar to this you can find here: https://zoptamo.com/uk/s-golf-c-uk-coach (button with text “add it”)