Any body can suggest to save a has many relation in yii.Since, i have seen some forum post which are suggesting to use extensions for it.But surely there will be any other solution for it.
I hope you guys can suggest me it.
Simply i have two relational table.
Table A has Many relation in Table B.
So, i have just define a relation in Model class of Table A.
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'records'=>array(self::HAS_MANY, 'Table_A','id'),
);
}
And in My controller action code i am just using a
$model->save();
To save this relational values.But What i am getting is here Table A record is created perfectly But There is n any record available in Table B
Might be a silly mistake i am doing here.Anybody who can suggest me how to save a HAS_MANY relation with Yii and without help of any extension.
You just have to loop through the related models to set the FK and save.
// save model A
$model_a->save();
// save related models
foreach($model_bs as $model_b ) {
$model_b->model_a_id = $model_a->id; // set the foreign key
$model_b->save();
}
But the difficult part is not the saving of them, but the gathering inputs of those related models with a form.
Sometimes you have to deal with one instance of model_a and multiple instances of model_b in a single form … not necessarily so, but sometimes it’s preferable for the user friendliness.
The starting point would be the following section of the guide.
softark, I know about this processs which called Collecting tabuler formate.But i was thinking a bit different Here.Since In CakePHP i have used a HAS_MANY relation and define a proper syntax for it in model Class.Then Calling a $model_a->save(); in the action save this relation and also it works like a charm On updating and Deleting operation.
But Why this usefull feature is still not included In Yii. I am going to send a feature request for it.
Yeah, I would also love to see a feature like that built in the core.
I don’t know why they didn’t do it so far. But the developers might have noticed some critical drawbacks in such things as a built-in feature of the core. I really don’t know.
i’m new to yii…am using these has_many relation to save data to database …while i search the result i found ur answer useful for me i think.but i had one doubt in this tabular input method…my question is that…the method getItemsToUpdate();i wrote in model file thst’s right?..but how i write the method i don’t have idea about it…so plz help me out to solve my issuses…
As "$this->" indicates, "getItemsToUpdate" method belongs to the same class as "actionBatchUpdate" method … it belongs to the controller class, not to the model class.
You may want to show a variable number of models, and eventually you would want to update the existing ones. But I think you’d be better start simple with a creation of a fixed number of the models.
public function getItemsToUpdate()
{
$items = array();
for ($i = 0; $i < 5; $i++) {
$items[] = new Item();
}
return $items;
}