Updating 2 tables which are related in one single form

Hi,

I have 2 tables DEPT and PROJECT which are related with a relation in the following way in Dept model:




	'projects' => array(self::HAS_MANY, 'Project', 'dept_id')



A single department can have to the most about 5 to 6 projects. I do not need to have CRUD features for the project table: I wish to be able to update the project table through a single form (view file) of the table department. For example, when I create or update a department, I have in the view file (form) a button <PROJECT> which opens a CJUI dialog where I can input info relative to the projects to be alloted to the department. The attribute dept_id of the record for the table <PROJECT> is given the value of the id of the department being updated.

Can anyone please give me a hint as to how to do this. I have tried the following:

deptController.php


public function actionAddnew() {

	                $project=new Project;

                        $project->dept_id= 2456 //inserted manually for testing purposes

	                $flag=true;

	        if(isset($_POST['Project']))

	        {       $flag=false;

	            $project->attributes=$_POST['Project'];

	 

	           if($project->save()) {

	           	echo "OKSAVE";

	                           

                        }

	                        

                        

	         }

	                if($flag) {

	                    Yii::app()->clientScript->scriptMap['jquery.js'] = false;

	                    $this->renderPartial('createDialog',array('model'=>$project,),false,true);

	                }

	        }	

View file (_form.php):




   	<?php echo CHtml::ajaxButton(Yii::t('dp','Project'),$this->createUrl('dept/addnew'),

    	array('onclick'=>'$("#pjDialog").dialog("open"); return false;','update'=>'#pjDialog'),

    	array('id'=>'showPjDialog'));?>		

			<div id="pjDialog"></div>	



_formDialog.php:




<div class="form" id="PjDialogForm">

 

<?php $form=$this->beginWidget('CActiveForm', array(

    'id'=>'project-form',

    'enableAjaxValidation'=>false,

)); 

?>

 

    <p class="note">Fields with <span class="required">*</span> are required.</p>

 

    <?php echo $form->errorSummary($model); ?>

 

    <div class="row">

        <?php //echo $form->labelEx($model,'date'); ?>

        <?php //echo $form->textField($model,'date',array('size'=>60,'maxlength'=>100)); ?>

        <?php //echo $form->error($model,'date');?>

        

    </div>

	<?php echo $form->labelEx($model,'remarks'); ?>

	<?php echo $form->textField($model,'remarks',array('size'=>10)); ?>

	<?php echo $form->error($model,'remarks'); ?>    

   

....    

....

....    

    <div class="row buttons">

        <?php echo CHtml::ajaxSubmitButton(Yii::t('dp','Save'),CHtml::normalizeUrl(array('dept/addnew','render'=>false)),array('success'=>'js: function(data) {

        						alert(data);

                        $("#pjDialog").dialog("close");

                    }'),array('id'=>'closePjDialog')); ?>

    </div>

 

<?php $this->endWidget(); ?>

 

</div>



createDialog.php




<?php 

$this->beginWidget('zii.widgets.jui.CJuiDialog',array(

                'id'=>'pjDialog',

                'options'=>array(

                    'title'=>Yii::t('Pj','Dept Projects'),

                    'autoOpen'=>true,

                    'modal'=>'true',

                    'width'=>'auto',

                    'height'=>'auto',

                ),

                ));

echo $this->renderPartial('_formDialog', array('model'=>$model)); ?>

<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>



How do I retrieve the dept id in the function addnew in the deptController? (I inserted it manually for testing purposes and it worked). Moreover how do I do it when I create a department since there is no dept id until the form is saved. I am proceeding step by step but ideally would like to have an editable grid (maybe) where all the projects of one department can be viewed, updated and deleted in a Cjui dialog something like the Cgridview. Can anyone please help? Thanks

PS. I have found a class found in extensions section: TabularInputManager.php. Where am I supposed to extract this file? It looks like this class could help do what I need.