Working with Foreign Keys in Forms

I have two tables Party.(PartyId (PK), DateCreated) and Organization.(OrganizationId (FK), Name). Using a form that includes both models I want my PartyId which is auto increment to be automatically populated in the OrganizationId field.

Here is my current actionCreate


public function actionCreate()

  {	

    $model=new Party;

    $organization = new Organization;


    if(isset($_POST['Party'], $_POST['Organization']))

    {

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

      $organization->attributes=$_POST['Organization'];

			

        if($model->save() && $organization->save())

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

					

			

      $valid=$model->validate();

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

    }


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

                'model'=>$model,

		'organization'=>$organization

      ));

}

What do I do from here to accomplish this?

Hi enfield.

If you are using giix in this project too, check GxActiveRecord::saveWithRelated. It may be useful for you in this case.

mentel, I will use giix for everything now! I will check out what you mention.

Please let me know how it worked :)

Maybe you can help me get there?

changed controller to:


public function actionCreate()

{	

  Yii::import('ext.giix-components.GXActiveRecord');

		

  $model=new Party;

  $organization = new Organization;


    if(isset($_POST['Party'], $_POST['Organization']))

    {

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

      $organization->attributes=$_POST['Organization'];

			

      $relatedData = array ('OrganizationId'=>$model->PartyId);

        if (GXActiveRecord::saveWithRelated($model, $organization, $relatedData))

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

					

      $valid=$model->validate();

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

    }


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

	    'model'=>$model,

	    'organization'=>$organization

	 ));

}

Now I get this error since transaction is set to true:


Property "PartyController.dbConnection" is not defined.

If I turn it to false in GXActiveRecord (not as you intended) I get:


PartyController does not have a method named "save".

This is just the start of getting this to work. I would like this turn this into a transaction. I already had that code done, wrapping both forms into a transaction, but with the FK issue I backed off to get that working.

It is not "GXActiveRecord". The correct is "GxActiveRecord".

Were you already using giix in this project? If so, the import call is unnecessary. Also, are the models extending from GxActiveRecord?

Yes I fat fingered that. Yes everything in this project was built with giix. I just doubled check to make sure everything was extending as it should be too. This is the one that I tried 1.8 on and then reverted to 1.7.

I didn’t really understand the import either but without it I get


Fatal error: Call to undefined function saveWithRelated() 

I even have this in my config file


'import'=>array( ...'application.extensions.giix-components.*',

If you don’t want to use extensions, etc. and want to do this with regular Yii, change your code around a bit. It looks like you were going down this route originally…why the [font=“Courier New”]$valid[/font] in the original code snippet if you never use it?


public function actionCreate()

{     

    $model=new Party;

    $organization = new Organization;


    if(isset($_POST['Party'], $_POST['Organization']))

    {

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

      $organization->attributes=$_POST['Organization'];

                        

      $valid=$model->validate();

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


      if ($valid) {

        if ($model->save()) {

          $organization->partyId = $model->id;

          if ($organization->save()) {

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

          }                                              

        }

      }

    }


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

                'model'=>$model,

                'organization'=>$organization

      ));

}

The snippet from Preston Brown will work for you (it is basically what giix does). You can try that.

Also, see the correct method signature:


public function saveWithRelated($relatedData, $runValidation = true, $attributes = null, $options = array())

Thanks Preston Brown and mentel. For now I just implemented Preston’s solution. I will revisit the other later.