P
P
Pavel Gogolinsky2014-08-16 15:59:30
Yii
Pavel Gogolinsky, 2014-08-16 15:59:30

How to combine create form and edit form?

There is an Artist model. She has a relation Song;
Each artist has only one song.
The controller has two actions.

public function actionCreate()
{
    $model = new Artist();
    $this->render('update', array(
        'model' => $model,
    ));
}
public function actionUpdate()
{
    $id = Yii::app()->request->getQuery('id');
    $model = Artist::model()->with('Song')->findByPk($id);
    $this->render('update', array(
        'model' => $model,
    ));
}

In view form for editing. I will show one field that gives an error
<?php $form->dropDownListGroup($model->song, 'name'); ?>

So, an error appears in this field when creating an artist, since with a newly created model, you cannot access $model->song. How to make it beautiful, so that for a new model this field would be empty?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander David, 2014-08-16
@alexdevid

<?php $form->dropDownList($model->song, 'name', CHtml::listData(Song::model()->findAll(), 'id', 'name')); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question