Tabular input...with a twist

Here’s what I’m trying to do:

I have a Job which has JobParts. The JobParts need certain Tasks to be performed. The Tasks are copies of Processes(where the name/description and other vital data comes from). Now some Processes have dependencies, meaning if Process-A is selected, a Task based on Process-B is automatically created as well.

However, I ran into an issue. Using $var->save() on any of the Tasks instances, creates a new Task for every instance. To be specific:

$tasks->save(); <-- saves $tasks

$dependency->save(); <-- saves $dependency AND $tasks

So if I have two dependencies, I will end up with two new Tasks for $tasks and one new Task for $dependency in my Tasks table.

public function actionStages($id) {

		$model = $this->loadModel($id, 'Jobs');

		if (isset($_POST['Tasks'])) {

                    foreach($_POST['Tasks'] as $i=>$task){


                            $tasks = new Tasks;



                                $tasks->job_id = $model->id;

                            $tasks->active = 1;

                            $tasks->cost_estimated = ($tasks->cost_estimated) ? $tasks->cost_estimated : 0;

                            $tasks->time_estimated = ($tasks->time_estimated) ? $tasks->time_estimated : 0;

                            $tasks->user_id = 1;


                            // Check for dependant processes


                                foreach ($tasks->process->processDepends as $d=>$depend){

                                    $process = $this->loadModel($depend, 'Processes');

                                    $dependency = new Tasks;

                                    $colors = explode('+', $tasks->jobPart->coloring);


                                    $dependency->time_estimated = ($process->productivity > 0) ? $process->productivity*$model->end_run : '';

                                    $dependency->cost_estimated = ceil($tasks->jobPart->print_sheets_volume*((int)$colors[0]+(int)$colors[1]))*$process->cost_each;

                                    $dependency->active = 1;

                                    $dependency->user_id = 1;

                                    $dependency->process_id = $depend;

                                    $dependency->job_id = $model->id;




                            // check if any extra data was input. If so, process it and store it in its table.


                                foreach($_POST['Tasks'][$i]['TaskData'] as $t=>$data){

                                    $taskdata = new TaskData;


                                    $taskdata->task_id = $tasks->id;







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


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

				'model' => $model,