madrani
(Hanifamustafida)
February 12, 2014, 11:00pm
1
hi…
i have this in my controller :
{
$model = Naskah::model()->find('id_naskah='.$id_naskah);
if(! empty($model)&&isset($_POST['Naskah']))
{
$model->no_referensi = $model->no;
$daftar_penerima = $_POST['Naskah']['daftar_penerima'];
foreach($daftar_penerima as $id_penerima)
{
$model=new Naskah;
$model->attributes = $_POST['Naskah'];
$model->id_penerima = $id_penerima;
$model->lampiran=CUploadedFile::getInstance
($model,'lampiran');
if($model->save())
{
if($file_name=$model->lampiran)
{
$file_name->saveAs(dirname
(Yii::app()->basePath) . '/files/'.$file_name);
$this->redirect(array('sent'));
}
}
.......
}
after submit in form, i want to save into multi rows in one table. how can i make it? help
selvakumar
(Selvakumar Kaliyappan)
February 13, 2014, 6:51am
3
I don’t have deep knowledge about this but, for my requirement i have used like this to save multiple values in the table.
I’m saving the education table values based on the user id so i have created as follows:
My controller:
$id = isset($_GET['u']) ? base64_decode($_GET['u']) : null;
$userModel= Users::model()->findByAttributes(array('id'=>$id));
$userEducations = $userModel->usersEducations;
if (isset($_POST['Users'])) {
$userModel->attributes = $_POST['Users'];
if (isset($_POST['UsersEducation'])) {
foreach($_POST['UsersEducation'] as $i => $education) {
if (count($userEducations) <= $i) {
$userEducations[$i] = new UsersEducation;
}
$userEducations[$i]->attributes = $education;
$userEducations[$i]->user_id = $id;
}
$isSaved = $userModel->save();
if ($isSaved) {
foreach($userEducations as $userEducation) {
$isSaved = $userEducation->save();
if (!$isSaved) {
break;
}
}
}
}
And based on your requirements you can change my code.,
Junior,
I don’t know how you manage to get multi rows from the view anyway dynamictabularform could you being of interest to you and I understand this can guide you.
Saluto.
P.D.: make use of this Multimodelform, to "Handling of multiple records and models in a form".
madrani
(Hanifamustafida)
February 15, 2014, 5:07am
5
Selvakumar:
I don’t have deep knowledge about this but, for my requirement i have used like this to save multiple values in the table.
I’m saving the education table values based on the user id so i have created as follows:
My controller:
$id = isset($_GET['u']) ? base64_decode($_GET['u']) : null;
$userModel= Users::model()->findByAttributes(array('id'=>$id));
$userEducations = $userModel->usersEducations;
if (isset($_POST['Users'])) {
$userModel->attributes = $_POST['Users'];
if (isset($_POST['UsersEducation'])) {
foreach($_POST['UsersEducation'] as $i => $education) {
if (count($userEducations) <= $i) {
$userEducations[$i] = new UsersEducation;
}
$userEducations[$i]->attributes = $education;
$userEducations[$i]->user_id = $id;
}
$isSaved = $userModel->save();
if ($isSaved) {
foreach($userEducations as $userEducation) {
$isSaved = $userEducation->save();
if (!$isSaved) {
break;
}
}
}
}
And based on your requirements you can change my code.,
Selvakumar:
Junior,
I don’t know how you manage to get multi rows from the view anyway dynamictabularform could you being of interest to you and I understand this can guide you.
Saluto.
P.D.: make use of this Multimodelform, to "Handling of multiple records and models in a form".
thank you very much…it SOLVED
mrs
(Raahsalah)
February 15, 2014, 5:37am
6
That is great.
It would be excellent if you can share your solution here then other Yii user can find this post helpful. It’s always appreciated.
Thanks