Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp 3 multi level associated save

I have a Users model which has a hasOne relationship to an Employees model. When I save a user I also add a record in the Employees table with the user_id. This works fine.

The Employee can be associated to belongsToMany Courses through CoursesEmployees table. This saves ok when I only save the Employees.

My problem is I want to save all 3 three but the Courses do not get saved.

Save user -> save employee with new user_id -> save courses chosen for new employee with employee_id in courses_employees

EmployeesTable.php

public function initialize(array $config)
{
    $this->table('employees');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',

    ]);
    $this->belongsTo('Hotels', [
        'foreignKey' => 'hotel_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsToMany('Courses', [
        'foreignKey' => 'employee_id',
        'targetForeignKey' => 'course_id',
        'joinTable' => 'courses_employees'
    ]);
}

users add.ctp:

<?= $this->Form->create($user) ?>
<fieldset>
    <legend><?= __('Add User') ?></legend>
    <?php
        echo $this->Form->input('role_id', ['options' => $roles, 'empty' => true]);
        echo $this->Form->input('email');
        echo $this->Form->input('active');
        echo $this->Form->input('activation_key');
        echo $this->Form->input('password');
        echo $this->Form->input('employee.name');
        echo $this->Form->input('employee.email');
        echo $this->Form->input('employee.surname');
        echo $this->Form->input('employee.employee_num');
        echo $this->Form->input('employee.courses._ids', ['options' => $courses]);
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

Here is the post data sent to the add method for save. With this the user and employee get saved correctly but no recored in the courses_employees.

If I save the employee form the employee controller the courses_employees get add ok so I know the associations are setup ok.

[
'role_id' => '1',
'email' => '[email protected]',
'active' => '11111111111',
'activation_key' => '1',
'password' => 'asdasdadasdasdasda',
'employee' => [
    'name' => 'asdasdasdasd',
    'email' => 'asdasdasd2dasd.com',
    'surname' => 'asdasdads',
    'employee_num' => 'asdasdadasdas',
    'courses' => [
        '_ids' => [
            (int) 0 => '2'
        ]
    ]
]
]

UserController.php

public function add()
{
    $this->loadModel('Courses');
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data, [
            'associated' => ['Employees', 'Courses']
        ]);
        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.'));
        }
    }
    $roles = $this->Users->Roles->find('list', ['limit' => 200]);
    $courses = $this->Courses->find('list', ['limit' => 200]);
    $this->set(compact('user', 'roles', 'courses'));
    $this->set('_serialize', ['user']);
}
like image 965
Keith Power Avatar asked Dec 14 '22 14:12

Keith Power


1 Answers

I have found the solution. I have read the cookbook again and I noted in one of the examples snippets 'associated' => ['Tags', 'Comments.Users']

I have applied this to my controller and it now works. It is saving on all levels.

$user = $this->Users->patchEntity($user, $this->request->data, [
    'associated' => ['Employees', 'Employees.Courses']
]);
like image 80
Keith Power Avatar answered Jan 08 '23 03:01

Keith Power