Creating new record in 2nd table, 2 models

Ok, very new to Yii - awesome framework and learning fast! A bit unsure how to do the following; this is what I have so far…

I have two tables in MySQL (client & job), both innoDB and a relational link one:many like so: client:id - job:clientid. It is set from within mysql using the foreign key.

(I also have a jobdet table linked to job table and holds 1:many enties as well - outside the scope of this question but here for more info)

I have modelled & crud’d both client and job, and have modified the client/admin’s gridview to have an additional button to allow a new job to be created;

In client/admin.php,




		array(

			'class'=>'CButtonColumn',

                        'template'=>'{view} {update} {delete} {newjob}',

                        'header'=>'Actions',

                        'buttons'=>array('newjob' => array(

                                        'label'=>'New Job',

                                        'url'=>'Yii::app()->createUrl("job/create",array("clientid"=>$data->id))',

                                        'imageUrl'=>Yii::app()->request->baseUrl.'/images/job.png',

                                ),

                        ),

                        'htmlOptions'=>array('width'=>75),


		),



The idea is there has to be an existing client record in order for a new job to be created for it. I figured the best way was to harness the existing client model to find or create a new client and then click the button to create the new job. Clicking this button jumps me to the job model where we start entering in the new job details - my issue is how we pass clientid to the job form and subsequent table…

I have tried the following (which does pass clientid & populates the form’s field) but I think is not very elegant and also I want the job form to show the client’s name (client:firstname & client:lastname), while at the same time placing clientid value to the table.

Also, if I am having issues hiding the field clientid because validation fails saying it cannot be empty.

In job/_form.php,




<div class="form">


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

	'id'=>'job-form',

	'enableAjaxValidation'=>false,

)); ?>




<?php

if(!empty($_GET['clientid'])) {

	$cid = $_GET['clientid'];

	echo 'DEBUG:cid = '.$cid;  // my debug - just to see if it works...

	$model->clientid=$cid;     // set the value here

} else {

 echo "DEBUG:cid not set!";

}

?>


etc...




My gutfeeling says there is a much more simpler or standard way of doing this…

Any ideas or a better way? Many many thanks in advance guys!

Noone? Any ideas anyone?

Ok, did abit more research and found I should(?) be putting my logic in the controller section rather than the view (job/_form.php) so now jobcontroller looks like the following;

In JobController.php




	public function actionCreate()

	{

		$model=new job;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

			$model->attributes=$_POST['job'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		if(!empty($_GET['clientid'])) {

			$cid = $_GET['clientid'];

			$model->clientid=$cid;

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}



Still running somewhat blind with respect to creating a new record in a child table that has a relation to an existing record in a parent table with one:many relation; any direction would be greatly appreciated.

You can create a form for the child table.

For the field foreign-key to the parent table you can use a CHtml::ActiveDropDownList() populated with the record in the parent table.

I do have a form for the child table ‘job’, it has been model’d and crud’d… but are you suggesting creating a form on the same page as the client gridview? Im abit confused…

The logic states I need to insert the id of the client in question into the job (field: clientid), thus I am passing the clientid in the url (eg. ‘?r=job/create&clientid=3’) when clicking on the respective client in the client gridview… whether this is the best way of performing this or not?

I also want the title of the form to show ‘Firstname Lastname - Organisation’ of the client at the top of the new job form.

Tables belows;

Table: client

id : mediumint(11)

organisation :varchar(100)

firstname :varchar(100)

lastname : varchar(100)

(etc)

Table: job

id : mediumint(11)

clientid : mediumint(11)

entereddate: datetime

jobtitle : varchar(128)

(etc)

UPDATE: I have since simplified my form to only adding in a jobdet with links to adding time and adding item enties since this needs to work on mobile platforms (ie iPhone) and did not want to employ jQuery here. In its ‘dumbed down’ state it is working nicely.

The way it works is all redirects after save() go back to the main job-client-jobdet-jobdet-jobdet_form page and links here allow a user to easily add/edit time/item entries including links to editing existing jobdet entries…

Anyhow, thought I’d update this thread on how I solved my issue… and many thanks to Zaccaria in providing assistance, it truly was appreciated!

Hi GoorooTech!

I don’t know if there is some best practice about how to pass parameters to child form, anyway I always use your same system (passing like a get parameter).

If you want to display the data of client, you can just add in controller:




$client= client::model()->findByPk($_GET['clientid']);



And pass this $client to the view, where you can write something like:




<h2><?php echo $client->firstname ?> <?php echo $client->lastname?> - <?php echo $client->organization?></h2>



I’m trying to accomplish the same exact thing, and ended up using the advice posted above. Are either of you guys having security issues with the id of the model being exposed? I realize that question may be well beyond the scope of this topic, but I’m a bit surprised there is not a better way to pass the parent (client) ID to the child (job) creation form.

Thanks!