There are four association types in CakePHP hasOne, hasMany, belongsTo, and belongsToMany. In this topics we will see how to save cakephp belong to many associative data or many to many relational data.
For create this example at first we will create database tables. We will create this many to many relationship example for engineers and skills.
For that we required three database tables . In the example below we would need tables for engineers, skills and engineers_skills.
Here , engineers_skills called Junction table. The engineers_skills table contains the data that links engineers and skills together. The joining table is named after the two tables involved, separated with an underscore by convention. In its simplest form, this table consists of engineer_id and skill_id.
Example below is our database table diagram.
First step, We will create models for engineers, skills and engineers_skills. For that we can use simple cakephp bake command to generate models for among tables.
bin/cake bake all engineers bin/cake bake model skills bin/cake bake model engineers_skills
Notice that we have created cake bake all engineers, because we will save belongs to many data from engineers add template.
So, after generated model by bake command our engineers association will looks like below , where engineers belong to many skills.
$this->belongsToMany('Skills', [ 'foreignKey' => 'engineer_id', 'targetForeignKey' => 'skill_id', 'joinTable' => 'engineers_skills', ]);
In engineer entity we have to allow access for skills
protected $_accessible = [ 'name' => true, 'created' => true, 'modified' => true, 'skills' => true, ];
Now, We will create a select box with multiple attribute to save many skills in engineers/add.ctp file.
To get skill list , In controller we can use find method to get all list of skills
$skills = $this->Engineers->Skills->find('list', ['limit' => 200]); $this->set(compact('skills'));
Now in Engineers/add.ctp we can create a field like below.
<? = $this->Form->control('skills._ids', ['options' => $skills]); ?>
<?php echo $this->Form->select('skills._ids', $skills, [ 'multiple' => true ]) ?>
Now after save, engineer_id and skill_id should save in engineers_skills table.
Now Let’s make it little complicated ! We want to put rating with skill for engineer. Example one engineer has 4.5 rating in PHP skill. So we want to save this rating in engineers_skills table. Same think you may face when you will try to create a shopping cart for saving the product quantity.
So, I have included rating field in table engineers_skills , It’s now looking like below
id || engineer_id || skill_id || rating
I have just removed multiple select box and created a field for skill and ratings, because I have per skill wise rating. You can use several html technique for create your form. Below I am going to give an easy example.
echo $this->Form->control('skills.0.id', [ 'type' => 'select', 'options' => $skills ]);
and also created an input filed for rating
Your request data should be like below after debug.
Now after save rating value you should able to see this value in your table.