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
));
}
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.
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()
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
));
}