Hi
I have a form which references multiple models, and in my controller I have a requirement to pass my $model->id to the referenced tables before i actually save my $model.
The reason for this is that I take the ID’s in my referenced tables and then pass them into my main $model when saving.
Is there a way to find the ‘next auto incrementing primary key’ value before I save my model?
The following is taken from my controller create.
if(isset($_POST['Venue']))
{
$model->attributes=$_POST['Venue'];
$modelBilling->attributes=$_POST['Billing'];
$modelGeo->attributes=$_POST['VenueGeo'];
//validation check
$valid = $model->validate();
$valid = $modelBilling->validate() && $valid;
$valid = $modelGeo->validate() && $valid;
if($valid)
{
// Billing
$modelBilling->chain_pays_billing=$model->venue_chain_pays_billing;
$modelBilling->save(); // save billing
// VenueGeo
$modelGeo->venue_id=$model->id; // pass the venue id ****
$modelGeo->save(); // save geo location
// RegionVenues
$modelRegion->venue_id=$model->id; // pass the venue id ****
$modelRegion->region_id=$model->region_venues_id;
$modelRegion->region_id1=$model->region_venues_id;
$modelRegion->save(); // save selected region venues
// if venue is part of a chain populate the chainVenues table
$x=$model->venue_part_of_chain; // load up the form value 'yes' or 'no'
if($x=='yes')
{
$modelChainVenue->venue_id=$model->id; // pass the venue id ****
$modelChainVenue->chain_id=$model->chain_id;
$modelChainVenue->chain_id1=$model->chain_id;
$modelChainVenue->save(); // save the ChainVenues
}
// VenueActiveSeason
$modelActiveSeason->venue_id=$model->id; // pass the venue id ****
$y=Yii::app()->db->getLastInsertId('LeagueActiveSeason'); // get the last inserted id
$modelActiveSeason->league_season_id=$y; // pass active season id
$modelActiveSeason->league_last_update_id=$y; // pass active season id
$modelActiveSeason->league_active_season_id=$y; // pass active season FK_id
$modelActiveSeason->save(); // save active season
// Venue ****
$model->billing_id1=$modelBilling->id; // get the billing ID and assign it to the FK_id
$model->venue_geo_id=$modelGeo->id; // pass the geo id to venue geo id
$model->chain_venues_id=$modelChainVenue->id; // pass ChainVenue id to venue chain_venues_id
$model->venue_active_season_id=$modelActiveSeason->id; // pass the active season id
$model->region_venues_id=$modelRegion->id; // pass the region id
$model->save(); // save venue
$this->redirect(array('view','id'=>$model->id));
}
}
The create functionality works, and is writing to the database tables apart from the ‘=$model->id;’ elements before I save the $model
Any help would be most appreciated
GPM