Yii Framework Forum: Relational Db Issues - Yii Framework Forum

Jump to content

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

Relational Db Issues Rate Topic: -----

#1 User is offline   jonathan1028 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 16-December 12

Posted 16 December 2012 - 09:51 PM

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,
));
}
0

#2 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 358
  • Joined: 23-July 09

Posted 27 December 2012 - 07:44 PM

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?
We were all once expert at....... nothing.

yii-language-behavior

My Blog
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