Yii Framework Forum: Creating new record in 2nd table, 2 models - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Creating new record in 2nd table, 2 models creating new entry in 2nd table, based on id of 1st table Rate Topic: -----

#1 User is offline   GoorooTech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 19-June 10
  • Location:Alstonville, NSW, Australia

Posted 19 June 2010 - 08:16 PM

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!
Jason
0

#2 User is offline   GoorooTech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 19-June 10
  • Location:Alstonville, NSW, Australia

Posted 20 June 2010 - 09:48 PM

Noone? Any ideas anyone?
Jason
0

#3 User is offline   GoorooTech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 19-June 10
  • Location:Alstonville, NSW, Australia

Posted 21 June 2010 - 08:43 PM

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.
Jason
0

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 June 2010 - 01:52 AM

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.
0

#5 User is offline   GoorooTech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 19-June 10
  • Location:Alstonville, NSW, Australia

Posted 22 June 2010 - 07:16 PM

View Postzaccaria, on 22 June 2010 - 01:52 AM, said:

You can create a form for the child 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..

View Postzaccaria, on 22 June 2010 - 01:52 AM, said:

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


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)
Jason
0

#6 User is offline   GoorooTech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 19-June 10
  • Location:Alstonville, NSW, Australia

Posted 11 July 2010 - 10:38 PM

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!
Jason
0

#7 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 12 July 2010 - 09:05 AM

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>

0

#8 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 22 July 2010 - 02:38 AM

View Postzaccaria, on 12 July 2010 - 09:05 AM, said:

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!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users