hi friends, this may be a very newbie question.
please help me.
i have two tables
profile
user_id | name | age | experince
And another table called
Experience
id | user_id | comapany | exp | from_year | to_year
how to save the data in both tables after user submits the registration??
hi Mubashar Iqbal thanks for the reply
actually, i have the post data of the registration form for these two models like these
$profile->attributes=((isset($_POST['Profile'])?$_POST['Profile']:array()));
//the data are
//$profile->user_id
//$profile->name
//$profile->age
//$profile->experience
And for the experince table the data are
$experince->attributes=((isset($_POST['workexp'])?$_POST['workexp']:array()));
//$experince->user_id
//$experince->comapany
//$experince->exp
----
if i save profile model first then the experience id in the profile model will be null because i haven’t saved the experience table to relate with this profile model.
And if i save the experince model first then i wont get the user_id to relate with the profile model. I am confused please help
you can use transaction to do this job, first you will save the data in user table and find the last insert id(user_id) mention below to find the last inserted id. and than save the data in experience table.
if($model->save())
{
// than you can get id just like that
$model->id // this is inserted item id
}
Hi,
Do you want to save the all data through profile? if so, follow the steps:
- create the profile form consisting the fields of Experience.
i.e.
....
<div class="row">
<?php echo $form->labelEx($model,'company'); ?>
<?php echo $form->textField($model,'company',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'company'); ?>
</div>
....
- declare variable into the model profile.
....
public $company
....
- in create action of the profile controller, set scenario variable of Experience
$model->setScenario('company');
- assign them (Experience’s variables) as safe into rules in the model of profile
array('company', 'safe'),
- in create action of the profile controller, instantiate Experience model, assign them (Experience’s variables) in the Experience attributes.
public function actionCreate()
{
$model=new Profile();
$model->setScenario('company');
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Profile']))
{
$model->attributes=$_POST['Profile'];
if($model->save())
if(isset($model->company))
$exp = new Experience();
$exp->user_id = $model->primaryKey;
$exp->company = $model->company;
if($exp->save())
$this->redirect(array('view','id'=>$model->user_id));
}
$this->render('create',array(
'model'=>$model,
));
}
- save and check the database, you’ll find the both table would be inserted.
Thanks