Saving a hasOne association in cakephp 3

2k views Asked by At

I have two Models(or Tables in Cakephp 3), one of them is Department and the other is Manager, I did a 'hasOne' association between those two:

Department Table (Database):

------------------------
| id | name | location |
------------------------ 
|  1 | IT   | New York |
------------------------

Manager Table (Database):

-----------------------------------------
| id | name | address  | department_id  |
-----------------------------------------
|  1 | Mike | New York |  1             |
-----------------------------------------

DepartmentsTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;

class DepartmentsTable extends Table {

    public function initialize(array $config){

    }

}

ManagersTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;

class ManagersTable extends Table {


    public function initialize(array $config){
       $this->hasOne('Departments');
    }

}

I created a Form so I can save a person with his address :

<?php echo $this->Form->create($manager); ?>
<?php echo $this->Form->input('Manager.name'); ?>
<?php echo $this->Form->input('Manager.address'); ?>
<?php echo $this->Form->input('Manager.department.name'); ?>
<?php echo $this->Form->input('Manager.department.location'); ?>
<?php echo $this->Form->end('valider'); ?>

and this is the controller :

<?php
namespace App\Controller\Adminocity;

use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Cache\Cache;
use Cake\ORM\TableRegistry;
use Cake\Event\Event;

class ManagersController extends AppController {

     public function edit_manager($id=null)
     {
        $this->loadModel('Departments');

        $manager = $this->Managers->newEntity();

        if($this->request->is('post')){

            $managersTable = TableRegistry::get('Managers');
            $manager = $managersTable->patchEntity($manager,$this->request->data(), ['associated' => ['Departments']]);


                if($managersTable->save($manager)){
                    $this->Flash->success(__('Manager ajouté avec succès'));
                    return $this->redirect(['action' => 'list_managers']);
                }
        }

        $this->set('manager', $manager);
     }
}

When I try to submit the form after filling it, the two new rows of 'Departments' and 'Manager' are added successfully, but the new row of 'Manager' has the value 'NULL' on 'department_id' column instead of the value of the id of the new 'Department' row recently created.

Please, can anyone help me solve this problem ?

1

There are 1 answers

0
José Lorenzo Rodríguez On BEST ANSWER

The relationship type should be belongsTo()

As a general rule, you can know when to use belongsTo or hasOne:

  • belongsTo: The foreign key is in the same table (Managers has a department_id)
  • hasOne: The foreign key is in the other table. (Department hasOne Manager)