Quentinb
(Quentin)
March 19, 2014, 9:58am
1
Hi all.
I have the following CheckBoxList.
<?php
echo $form->checkBoxList($model,'sessionsids',ConferenceProgram::model()->getConfListData(),
array(
'template'=>'{input}{label}',
'separator'=>'<br>',
'labelOptions'=>array(
'style'=> '
width: 690px;
float: right;
'),
) );
?>
I got the example for somewhere on the net. As you can see I hook it to my current view model called $model, but the actual list comes from ConferenceProgram::model()->getConfListData()
The list works and my checkboxes are shown, but on submit of the form, it is not getting any data back. I have tried:
$model->attributes=$_POST['ConfConferenceAttendee'];
//Both lines below return nothing
$sessionsarr=$model->sessionsids;
$sessionsarr=$_POST['ConferenceAttendee']['sessionsids'];
And a few other things, but I just don’t get the selected checkbox values back to the controller.
Thanks for any help…
konapaz
(Konapaz)
March 19, 2014, 10:17am
2
Hi
did you set the sessionsids in rules of the model (and as a specific variable) ?
Also trace your code using var_dump($_POST). What do you see ?
Quentinb
(Quentin)
March 19, 2014, 10:32am
3
This is what I have created in ConferenceAttendee model:
public $sessionsids = array();
public function rules()
{
return array(
array('sessionsids', 'required','message'=>'You must select at least 1 Conference Session.'),
);
}
public function getSessionsIds()//Custom field
{
return $this->sessionsids;
}
public function setSessionsIds($sessionsids)//Custom field
{
$this->sessionsids = $sessionsids;
}
This is the var_dump output:
array (size=2)
'ConfConferenceAttendee' =>
array (size=10)
'person_firstname' => string 'Katrin' (length=6)
'person_lastname' => string 'ouioiu' (length=6)
'person_contact_email' => string 'katrin@uoiuio.com' (length=17)
'person_contact_number' => string '8889996235' (length=10)
'address_line_1' => string 'adr1' (length=4)
'address_line_2' => string 'adr2' (length=4)
'address_line_3' => string 'adr3' (length=4)
'address_line_4' => string 'adr4' (length=4)
'conf_diet_type_id' => string '2' (length=1)
'sessionsids' =>
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
'yt0' => string 'Save' (length=4)
konapaz
(Konapaz)
March 19, 2014, 10:57am
4
Quentinb:
This is what I have created in ConferenceAttendee model:
public $sessionsids = array();
public function rules()
{
return array(
array('sessionsids', 'required','message'=>'You must select at least 1 Conference Session.'),
);
}
public function getSessionsIds()//Custom field
{
return $this->sessionsids;
}
public function setSessionsIds($sessionsids)//Custom field
{
$this->sessionsids = $sessionsids;
}
This is the var_dump output:
array (size=2)
'ConfConferenceAttendee' =>
array (size=10)
'person_firstname' => string 'Katrin' (length=6)
'person_lastname' => string 'ouioiu' (length=6)
'person_contact_email' => string 'katrin@uoiuio.com' (length=17)
'person_contact_number' => string '8889996235' (length=10)
'address_line_1' => string 'adr1' (length=4)
'address_line_2' => string 'adr2' (length=4)
'address_line_3' => string 'adr3' (length=4)
'address_line_4' => string 'adr4' (length=4)
'conf_diet_type_id' => string '2' (length=1)
'sessionsids' =>
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
'yt0' => string 'Save' (length=4)
So after of that
$model->attributes=$_POST[‘ConfConferenceAttendee’];
var_dump ($model->sessionsids) should display all selected items. right ?
Quentinb
(Quentin)
March 19, 2014, 11:13am
5
Yip, after doing:
var_dump ($model->sessionsids);
die;
I get:
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
This is 100% correct, but how to access them so I can write them to the database?
konapaz
(Konapaz)
March 19, 2014, 11:16am
6
Quentinb:
Yip, after doing:
var_dump ($model->sessionsids);
die;
I get:
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
This is 100% correct, but how to access them so I can write them to the database?
You should override the beforeSave or afterSave model method and in foreach iterator save the another models with the specific sessionsid
Quentinb
(Quentin)
March 19, 2014, 12:12pm
7
Thanks, but as I have mentioned, I cannot seem to get the ids back to my action using any of the below methods to access them. I can see via the DUMP that they are getting passed, but how do I get them stored in a variable so I can use them? Below is what I have tried:
None of these work.
$sessionsarr=$model->sessionsids; //sessionsids is empty
$sessionsarr=$_POST['ConferenceAttendee']['sessionsids']; //sessionsids is empty
$sessionsarr=$_POST['sessionsids']; //Compilation error
This is what I use to iterate through them:
for($i=0; $i<sizeof($sessionsarr); $i++)
{
$sessStore = new ConfAttendeeLinkProgram; //Model
$sessStore->attendee_id=$id; //$id is the primary key of selected row to update
$sessStore->conference_program_id=$sessionsarr[$i];
$sessStore->save();
}
konapaz
(Konapaz)
March 19, 2014, 12:26pm
8
Quentinb:
Thanks, but as I have mentioned, I cannot seem to get the ids back to my action using any of the below methods to access them. I can see via the DUMP that they are getting passed, but how do I get them stored in a variable so I can use them? Below is what I have tried:
None of these work.
$sessionsarr=$model->sessionsids; //sessionsids is empty
$sessionsarr=$_POST['ConferenceAttendee']['sessionsids']; //sessionsids is empty
$sessionsarr=$_POST['sessionsids']; //Compilation error
This is what I use to iterate through them:
for($i=0; $i<sizeof($sessionsarr); $i++)
{
$sessStore = new ConfAttendeeLinkProgram; //Model
$sessStore->attendee_id=$id; //$id is the primary key of selected row to update
$sessStore->conference_program_id=$sessionsarr[$i];
$sessStore->save();
}
You said that the var_dump $_POST[‘ConfConferenceAttendee’][‘sessionsids’] has values so,
$sessionsarr = $model->sessionsids; //if not works then something not works in massive assigment on
$model->attributes = $_POST[‘ConfConferenceAttendee’]; (please inform us)
$sessionsarr=$_POST[‘ConferenceAttendee’][‘sessionsids’]; //has values ?
Quentinb
(Quentin)
March 19, 2014, 12:44pm
9
Thank you very much, by adding more DUMPS I could trace it down to the loop and followed your instruction to use a foreach loop instead.
To finish and close this topic, this is the final piece of code:
if(isset($_POST['ConfConferenceAttendee']))
{
$model->attributes=$_POST['ConfConferenceAttendee'];
$sessionsarr = $model->sessionsids;
if($model->save())
{
foreach($sessionsarr as $sessid)
{
$sessStore = new ConfAttendeeLinkProgram;
$sessStore->attendee_id=$id;
$sessStore->conference_program_id=$sessid;
$sessStore->save();
}
Quentinb
(Quentin)
March 19, 2014, 1:22pm
11
It seems like it did not like the for loop, or the for loop did not understand the $sessionarr, or maybe $sessionsarr was/is not passed as an array?
This worked:
$sessionsarr = $model->sessionsids;
foreach($sessionsarr as $sessid)
{
$sessStore = new ConfAttendeeLinkProgram;
$sessStore->attendee_id=$id;
$sessStore->conference_program_id=$sessid;
$sessStore->save();
}
This did NOT work:
$sessionsarr = $model->sessionsids;
for($i=0; $i<sizeof($sessionsarr); $i++)
{
$sessStore = new ConfAttendeeLinkProgram;
$sessStore->attendee_id=$id;
$sessStore->conference_program_id=$sessionsarr[$i];
$sessStore->save();
}
konapaz
(Konapaz)
March 19, 2014, 2:20pm
12
Quentinb:
It seems like it did not like the for loop, or the for loop did not understand the $sessionarr, or maybe $sessionsarr was/is not passed as an array?
This worked:
$sessionsarr = $model->sessionsids;
foreach($sessionsarr as $sessid)
{
$sessStore = new ConfAttendeeLinkProgram;
$sessStore->attendee_id=$id;
$sessStore->conference_program_id=$sessid;
$sessStore->save();
}
This did NOT work:
$sessionsarr = $model->sessionsids;
for($i=0; $i<sizeof($sessionsarr); $i++)
{
$sessStore = new ConfAttendeeLinkProgram;
$sessStore->attendee_id=$id;
$sessStore->conference_program_id=$sessionsarr[$i];
$sessStore->save();
}
It should work!
$sessionsarr is an array with integer index (0=>‘1’,1=>‘3’,…)
check with var_dump ($sessionsarr); ‘foreach’ and ‘for’ should work same loops, add an echo ($i); what happens ?