I have a quick beginners question.
Is this the right way to deal with two models and one form in yii2 also? http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/
Or to be more precise, with two models that are related with hasMany.
?
I have tried this but I get error
Call to a member function formName() on a non-object
This is in my form
<?= $form->field($model->translations, 'title')->textInput(['maxlength' => 8]) ?>
And this is in my model
public function getTranslations()
{
return $this->hasMany(Translation::className(), ['article_id' => 'id']);
}
Can someone help me? I’m stuck with this…
Vojtech
(Vojtech Horak)
August 1, 2014, 3:18pm
4
I’m not sure about what are you trying to achieve. If you would like to select related model (by using Drop-down List) you might need something like this:
<?= $form->field($model, 'article_id')->dropDownList(ArrayHelper::map(Translation::find()->all(), 'id', 'name')) ?>
I have two tables, article and translation. They are related in Article model like this
public function getTranslations()
{
return $this->hasMany(Translation::className(), ['article_id' => 'id']);
}
Now, when creating new article I have some fields that I insert into article table, but also have few fields that I need to insert into translation table.
I would like to do this with one form.
tuga
(Tuga74)
August 1, 2014, 3:55pm
6
So you need to have 2 models,one for Translation and one for Articles.
Is that the right way?
In my controller I currently have
public function actionKret()
{
$model = new Article;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('kret', [
'model' => $model,
//'translation' => $translation,
]);
}
}
And in my form
.....
<?= $form->field($model, 'upvote')->textInput(['maxlength' => 8]) ?>
<?= $form->field($model, 'translations[title]')->textInput(['maxlength' => 8])->label('Title') ?>
<?= $form->field($model, 'translations[slug]')->textInput(['maxlength' => 8])->label('Slug') ?>
....
tuga
(Tuga74)
August 1, 2014, 4:04pm
8
Dont’ think so…maybe something more like this:
public function actionKret()
{
$modelArticle = new Article;
$modelTranslation = new Translation;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('kret', [
'modelArticle' => $model,
'modelTranslation' => $translation,
]);
}
}
and then
.....
<?= $form->field($modelArticle, 'upvote')->textInput(['maxlength' => 8]) ?>
<?= $form->field($modelTranslation, 'title')->textInput(['maxlength' => 8])->label('Title') ?>
<?= $form->field($modelTranslation, 'slug')->textInput(['maxlength' => 8])->label('Slug') ?>
....
Then ofcourse you have to do 2 saves, maybe…
$modelArticle->save();
$modelTranslation->articileID = $modelArticle->id; // just a guess for the names
$modelTranslation->save();
Hm, okay.
I thought that in Yii2 this will be handled somehow automatically with
public function getTranslations()
{
return $this->hasMany(Translation::className(), ['article_id' => 'id']);
}
OK, thanks