Relational Db Issues

Hi, I have a website with a Builder model. The idea is that the Builder creates multiple subcontractors under their profile and then has them displayed to interact with. I wrote the code below and can get the Subcontractor created through the Builder model, but no data is being generated to by JBuildSub table. My understanding is that I need to create pairs of builderID/subcontractorID in a joint table so that I can then draw back into my Builder model the unique subcontractors that apply to that specific Builder. I don’t understand why this JBuildSub table won’t take the data. It has only two columns in it of: builderId and subcontractorId.

I’ve read over and over all the documentation and tutorials I can find on the Yiiframework website so please don’t refer back to one of those documents. I need further clarification on how to get this working properly.

Thanks so much for any help you can provide.

public function actionCreateSub()

{

$model=new Subcontractor;


$joint=new JBuildSub;





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


{


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


	$joint->subcontractorId = $model->id;


	$joint->builderId = $this->id; 


	$joint->save();


	if($model->save())


		$this->redirect('subcontractors');


}





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


		'model'=>$model,


	));

}

I’m surprised that nobody has bothered to answer you :huh:

To begin, you are trying to set $joint->subcontractorId = $model->id when there is not yet a $model->id. This does not exist until after $model->save().

All your code to create $joint should go within this block of code:




if($model->save()) 

{


    // create your $joint here


    $this->redirect('subcontractors');

}



I am not at all sure about $joint->builderId = $this->id; Where is $this->id set?