Reputation: 141
Category Subcategory Mapping
id id Category_id
name name Sub_Category_id
I have created models and controllers for category and subcategory . How to insert data into the third table "Mapping" whenever I am adding new subcategory??
SubCategory form is having 3 fields.
1.DropDownList Category_Name 2.TextField Sub Category Name 3.Auto Generated Id of Subcategory
Upvotes: 1
Views: 43
Reputation: 3103
I do not see a problem, maybe you could share what you have tried. It should basically work like this (not tested)
class Subcategory{
...
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord) {
$map = new Mapping();
$map->Category_Id = $this->parentCategory->id;
$map->Sub_Category_Id = $this->id;
$map->save();
}
}
...
}
this is assuming that you have a relation from Subcategory
to Category
called parentCategory
Upvotes: 2