Multi form, multirecord entry screen

I have come to a cross roads with my app and not sure how to proceed with the form I want to create. The form needs to display Client and Job information & numerous job detail records, then allow the user to enter new jod details plus add time and item records at the same time. Attached is the database layout.

‘CLIENT’ table : holds client information such as organisation name, contact details, address etc

‘JOB’ table : holds details on summary & task information + organised date/time for job, assigned to who etc

‘JOBDET’ table : everytime we do something to a job it gets recorded in a jobdet record.

‘TIME’ table : we store time charged here if the jobdet was chargable.

‘ITEM’ table : same as time table but for hardware used

I want a page that can show and do the following;


CLIENT <— static text, basic client info here


JOB <— static text, basic job details here, jobdesc (basically tasks needing done)


JOBDET <— the next five jobdet records are text only (what we did on that visitation)


JOBDET

TIME <— this jobdet has a time record, we charged the client


JOBDET

TIME <— this jobdet has a time record, we charged the client + a part

ITEM


JOBDET <— another jobdet entry with 2 x time entries attached

TIME

TIME


JOBDET


form:JOBDET <— blank form to add another jobdet entry

form:TIME <— blank form to add a time record (NOT mandatory)

form:ITEM <— blank form to add a item record (NOT mandatory)


With respect to the TIME and ITEM forms, we need to be able to submit none or multiples ie a checkbox in front of the form to allow submission and a (+) to add a new form to be added underneath. So submission will be 1 jobdet record & 0+ time and 0+ item records.

Example;


ThisCompanyCo Pty/Ltd

156 Annadale St Lismore NSW 2333

Fred Smith - Ph(02) 6666 2222


Assignedto: Robert

OrgDate: 25/6/2010 09:00 - 11:00

Summary: Fix Printer, misc tasks

TaskDesc:

1/ Printer not working

2/ 1 x workstation not on Internet

3/ Tasks as directed by client


jobdet: Called client, discussed issues, organised onsite time


jobdet: 25/6/2010; Onsite, resolved printer issues, need 2nd visit

time: 1hr/$88


jobdet: 26/6/2010; 2nd visit, fixed Internet issues, dead network switch.

time: 2hrs/$88

item: 1 x network switch, $35


jobdet: Discussed remaining issues on phone, pended till they have freetime.


form-jobdet: _____________ ______ etc

[ ] time ____________ (+)

[ ] item ____________ (+)

**** the [ ] is a checkbox logic to allow submission, the (+) is code to create another form of either time or item.

Hopefully this give the general idea… Any thoughts on how to do this?! And thanks!

I’m working on a similar problem right now (also new to Yii), except coming at it from the viewing angle more than the entry angle. I’m basing my code on the Post/Comment relationship from the blog example, since this shows how to have multiple views on a single page with a form for entering a new item as well.

I’m assuming (foolishly?) that the blog example follows some sort of best practice or convention in the way that it structures the viewing and editing of multiple pieces of data.

Yeah, im am also very new to Yii and MVC conventions. Still pretty curious if my form can be achieved and how to tackle it. The dynamic addition of time/item forms for additional entry is another concern since I need to be able to enter none, 1, or more of each.

fwiw, I think the blog example is actually quite accurate of what we want and I will now be following this as my base. However when it comes to displaying my forms for input I’m pretty clueless and still need a course of action ie CForms, CActiveForm, or …

From what I’ve researched it may just have to be CForms due to the multiple tables we are inserting into…

For manage many record in a form you should collect tabular input.

For collect data for different tables you have simply to collect data in many modules.

for example:





public function actionUpdate()

{

	$client=$this->loadClient();

	$job=$this->loadJob();

	$jobdets=array();


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

	{

		$client->attributes=$_POST['Client']; //collect the data for client

		$job->attributes=$_POST['Job']; //collect the data for job

				

		foreach ($_POST['Jobdet'] as $id=>$jobdet)

		{

			$jobdets[$id]->loadJobdet($id);  // collect the input from all jobdets

			$jobdets[$id]->attributes=$_POST['Jobdet'][$id];

		}

		

		if ($this->weShouldValidate)

		{

			$valid=$client->validate();

			$valid=$job->validate() & $valid;

			foreach ($jobdets as $jobdet)

				$valid=$jobdet->validate() & $valid;

				

			if($valid)

			{

				$client->save(); //save all

				$job->save();

				foreach ($jobdets as $jobdet)

					$jobdet->save();

				

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

			}

		}

	}


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

		'client'=>$client,

		'job'=>$job,

		'jobdets'=>$jobdets,

	));

}



This is just a simple example. This allows you to update all existing records, but maybe you should want to give some button for add/delete a jobdett (that’s why ‘shouldWeValidate’, if we have just added a new record we should not validate, of corse the field of the new record will be empty).

That is a good starting point, I guess

This code looks awesome, thanks mate, will give it a go using this as an example. Really appreciate your time spent helping us.

Thank you very much for the above code…

I do have written something similar, and my code works.

with regards to the above example http://www.yiiframework.com/forum/index.php?/topic/10159-multi-form-multirecord-entry-screen/page__view__findpost__p__50124

            if (&#036;this-&gt;weShouldValidate)


            {


                    &#036;valid=&#036;client-&gt;validate();


                    &#036;valid=&#036;job-&gt;validate() &amp; &#036;valid;


                    foreach (&#036;jobdets as &#036;jobdet)


                            &#036;valid=&#036;jobdet-&gt;validate() &amp; &#036;valid;


                            


                    if(&#036;valid)


                    {


                            &#036;client-&gt;save(); //save all


                            &#036;job-&gt;save();


                            foreach (&#036;jobdets as &#036;jobdet)


                                    &#036;jobdet-&gt;save();


                            


                            &#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


                    }


            }

$client saves and updates well and $jobdets saves and updates well,

[list=1]however $job is not updating

[/list]

Can you please help?

Maybe you have to set some more attribute in client (like, for example, some foreign key id) before save.

Thank you very much for your prompt response. I really appreciate that. I will have a look in that area. :D

Hi Zaccaria,

I had a look at the code again and now I know why my updates were not saving… :rolleyes:

Sticking to the above example (http://www.yiiframework.com/forum/index.php?/topic/10159-multi-form-multirecord-entry-screen/page__view__findpost__p__50124) for simplicity and clarity sake;

When I wrote function loadJob




        private function loadJob()

	{

               if($this->_job === null)

		{

			if(isset($_GET['id']))

                            $criteria=new CDbCriteria;

                            $criteria->condition='job_id=:job_id';

                            $criteria->params=array('job_id'=>$_GET['id']);

                            $this->_job = Job::model()->find($criteria);

			if($this->_job===null)

				throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));

		}

                return $this->_job;

	}




Please note how I called find using this line and it loads but does not update


$this->_job = Job::model()->find($criteria);

However when I call it like below it loads and updates… :D


$this->_job = CActiveRecord::model('Job')->find($criteria);

I glad it works, but does anyone knows why? :blink:

Best Regards

Seal

Not being an expert, but I think both CActiveRecord and calling the model should produce the same result. Have you check your model case? i assume "Job::model()" and "job::model()" means different things. And your line

“$criteria->params=array(‘job_id’=>$_GET[‘id’]);” miss out “:” infront of job_id. Chears!~