As for that code… it goes where you want to save the data… so it goes in the controller… the _form file is the view… there is the code that shows the form on the page…
I was busy and couldn’t experiment with this. Now I’m trying it and I can’t seem to get the “other” values to save. Only the first one.
So I got my controller code like this:
public function actionCreate()
{
$model=new Names;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Names']))
{
$model->attributes=$_POST['Names'];
$name = explode('/n', $model->name);
foreach($name as $model->name)
if($model->save())
$this->redirect(array('view', 'id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
Can anyone point the error in my code?
The /n is for a new line (pressing enter on keyboard)
So if I enter in textArea:
Apple
Orange
Only Apple will save and Orange isn’t even passed/saved. Not sure why.
public function actionCreate()
{
$model=new Names;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Names']))
{
$model->attributes=$_POST['Names'];
$names = explode('/n', $model->name);
$allSaved = true;
foreach($names as $name)
if(!empty($name)) // don't save empty lines
{
$newName=new Names; //That's what you want: an extra model for each name
$newName->name = $name;
if(!$newName->save())
{
$allSaved = false;
break; //abort on first error (or continue here if you want to save other models)
}
}
if($allSaved)
$this->redirect(array('view', 'id'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?)); //what model(s) do you want to show here: allnew, the latest, the first <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />
}
$this->render('create',array(
'model'=>$model,
));
}
But there is more work:
What do you want to show on ‘view’ afterwards?
->All new models or the one with the collected names?
You have to use a ‘groupId’ or ‘flag’ to show all new inserted.
What do you want to show on update?
->Maybe you have to implement the onAfterFind() to collect multiple names into one textarea
The problem is, your db-model should not be the same as your form-model.
You should implement an extra ‘FormModel’ for the actionCreate (with the textarea) and work with (maybe master/detail) in the db (has_many relation).