Thanks! I’ve read through the documentation and am still having troubles. Any help you can offer is greatly appreciated.
My particular data structure is this:
-
the parent model, Staff, contains basic personal information (name, phone, etc)
-
there are many child models for Staff that are optional (zero to many records)
Education is one such child model. It is a simple text field in which the user can input information about when/where they went to school. The Education model has a (hidden) field, staffId, which links it back to the parent model, Staff.
I’d like the input to be a single page with multiple fieldsets. The first fieldset would have all necessary fields to complete the Staff model. The next fieldset would be for Education listing all the rows associated with the particular staffer. The first row of this table would be an editable field such that the user can add a new Education listing.
So, I need to populate a form using multiple models, and, after submission, create/update/delete the necessary information in multiple models.
The output of this is a form correctly populated with the Staff model information. However, the Education model fields are nowhere to be found? I realize in actionUpdate() I am not passing the array of Education models ($education) to the CForm object ($form)…that is because I don’t know how to pass it? I’ve tried looking at the CForm code but don’t see where it accepts multiple models.
Here is the code I have so far:
protected/controller/StaffController.php:
public function actionUpdate()
{
$staff=$this->loadModel();
$education=education::model()->findAllByAttributes(array('staffId'=>$staff->ID));
$form=new CForm('application.views.staff.staffForm',$staff);
if(isset($_POST['staff']))
{
$model->attributes=$_POST['staff'];
$model->updateTS = date('Y-m-d H:i:s');
if($model->save())
$this->redirect(array('view','id'=>$model->ID));
}
$this->render('inputStaff', array('form'=>$form));
}
protected/views/staffForm.php:
<?php
return array(
'elements'=>array(
'staff'=>array(
'type'=>'form',
'title'=>'Staff Information',
'elements'=>array(
'id'=>array(
'type'=>'hidden',
),
'name'=>array(
'type'=>'text',
),
'phone'=>array(
'type'=>'text',
),
'fax'=>array(
'type'=>'text',
),
'email'=>array(
'type'=>'text',
),
'website'=>array(
'type'=>'text',
),
),
),
'education'=>array(
'type'=>'form',
'title'=>'Education Information',
'elements'=>array(
'id'=>array(
'type'=>'hidden',
),
'staffId'=>array(
'type'=>'hidden',
),
'label'=>array(
'type'=>'text',
),
),
),
),
);
?>
protected/views/inputStaff.php:
<h1>Staff</h1>
<div class="form">
<?php
echo $form;
?>
</div>