Hi I am new to Yii and I have set up multi page forms (from Instant Building Multi-Page forms from PACT publishing) to add a new record with the actionCreate function in the controller. This is working fine. I would like the use the same forms with the actionUpdate function. Using $model=$this->loadModel($id); before rendering _page1 but subsequent pages will overwrite any changes made on previous forms if used again.
The code with 4 question marks in the actionUpdate function don’t work and have tried many different things and not sure what to try next. Any help is greatly appreciated, thank you!
The controller code is
public function actionCreate()
{
if(isset($_POST['page1']))
{
$model = new Listing('page1');
$this->checkPageState($model, $_POST['Listing']);
$view = '_page1';
}
else if(isset($_POST['page2']))
{
$model = new Listing('page1');
$this->checkPageState($model, $_POST['Listing']);
if($model->validate())
{
$view = '_page2';
$model->scenario = 'page2';
}
else
{
$view = '_page1';
}
}
else if(isset($_POST['page3']))
{
$model = new Listing('page2');
$this->checkPageState($model, $_POST['Listing']);
$model->user_id = Yii::app()->user->id;
$model->status = 'Active';
$model->date_listed = date('Y-m-d');
$model->reference = 'L-'.date('Y-m-d').'-'.($model->product_sale === 'Buy'?'B':'S').($model->product_type === 'Wine Grapes'?'G':'W').'...';
if($model->validate())
{
$view = '_page3';
$model->scenario = 'page3';
}
else
{
$view = '_page2';
}
}
else if(isset($_POST['submit']))
{
$model = new Listing('page3');
$model->attributes = $this->getPageState('page', array());
$model->attributes = $_POST['Listing'];
if($model->validate())
{
$model->save();
$model->reference = 'L-'.date('Y-m-d').'-'.($model->product_sale === 'Buy'?'B':'S').($model->product_type === 'Wine Grapes'?'G':'W').$model->id;
$model->save();
//Yii::app()->listing->setFlash('success', 'Create New Listing Successful');
$this->redirect(array('view', 'id'=>$model->id));
}
$view = '_page3';
}
else
{
$model = new Listing('page1');
$view = '_page1';
}
$this->render($view, array('model'=>$model));
}
public function actionUpdate($id)
{
if(isset($_POST['page1']))
{
//$model = Listing($scenario = 'page1'); <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?
// $model->attributes=$_POST['Listing']; <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?
$this->checkPageState($model, $_POST['Listing']);
$view = '_page1';
}
else if(isset($_POST['page2']))
{
//$model = Listing($scenario = 'page1'); <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?
// $model->attributes=$_POST['Listing']; <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?
$this->checkPageState($model, $_POST['Listing']);
if($model->validate())
{
$view = '_page2';
$model->scenario = 'page2';
}
else
{
$view = '_page1';
}
}
else if(isset($_POST['page3']))
{
//$model = new Listing($scenario = 'page2'); <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?
// $model->attributes=$_POST['Listing']; <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?
$this->checkPageState($model, $_POST['Listing']);
if($model->validate())
{
$view = '_page3';
$model->scenario = 'page3';
}
else
{
$view = '_page2';
}
}
else if(isset($_POST['submit']))
{
$model->attributes=$this->getPageState('page', array());
$model->attributes=$_POST['Listing'];
if($model->validate())
{
//may have to use $model->update(); not $model->save();
$model->update();
$model->reference='L-'.date('Y-m-d').'-'.($model->product_sale === 'Buy'?'B':'S').($model->product_type === 'Wine Grapes'?'G':'W').$model->id;
$model->update();
//Yii::app()->listing->setFlash('success', 'Update Listing Successful');
$this->redirect(array('view', 'id'=>$model->id));
}
$view = '_page3';
}
else
{
$model=$this->loadModel($id);
$view = '_page1';
}
$this->render($view, array('model'=>$model));
}
private function checkPageState(&$model, $data)
{
$model->attributes = $this->getPageState('page', array());
$model->attributes = $data;
$this->setPageState('page', $model->attributes);
}