Yii Framework Forum: Saving A Diferent Record After The Save Method - Yii Framework Forum

Jump to content

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

Saving A Diferent Record After The Save Method Rate Topic: -----

#1 User is offline   sanat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 26-September 12
  • Location:Caracas, Venezuela

Posted 01 February 2013 - 09:28 PM

Hi community,

I'm trying to save a record to a table B (I have defined Model B with Gii), in my method ActionCreate of ControllerTableA after the save method which save the current record in TableA.
When I did the code listed below, I had a record with null values in my tableB.
Of course the record saved in TableA was correct.
The TableA is parent of TableB and the fields of TableB are also in TableA.
TableB has some fields with values of TableA plus some other constant values.

Here is the code in ControllerTableA:

private $_tablaB;

public function actionCreate()
{
$model=new TABLE_A;
if(isset($_POST['tableA']))
{
$model->attributes=$_POST['tableA'];
if($model->save()) {
$this->_tableB = new tablaB; Model TableB
$this->_tableB->attributes = array('id'=>NULL, item1=>$model->id_item1, 'id_tablaA'=>model->ID,);
$this->_tablaB->save();
$this->redirect(array('view','id'=>$model->iD));
}
$this->render('create',array( 'model'=>$model, ));
}

What I'm doing wrong? Any help is appreciated
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 02 February 2013 - 01:36 AM

Hi sanat,

View Postsanat, on 01 February 2013 - 09:28 PM, said:

$this->_tableB->attributes = array('id'=>NULL, item1=>$model->id_item1, 'id_tablaA'=>model->ID,);


This will set only 3 fields ('id', 'item1' and 'id_tableA') in the tableB.
Probably you have to add the following line before it:
$this->_tableB->attributes = $_POST['tableA'];

0

#3 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 394
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 02 February 2013 - 11:49 AM

View Postsanat, on 01 February 2013 - 09:28 PM, said:

Hi community,

I'm trying to save a record to a table B (I have defined Model B with Gii), in my method ActionCreate of ControllerTableA after the save method which save the current record in TableA.
When I did the code listed below, I had a record with null values in my tableB.
Of course the record saved in TableA was correct.
The TableA is parent of TableB and the fields of TableB are also in TableA.
TableB has some fields with values of TableA plus some other constant values.

Here is the code in ControllerTableA:

private $_tablaB;

public function actionCreate()
{
$model=new TABLE_A;
if(isset($_POST['tableA']))
{
$model->attributes=$_POST['tableA'];
if($model->save()) {
$this->_tableB = new tablaB; Model TableB
$this->_tableB->attributes = array('id'=>NULL, item1=>$model->id_item1, 'id_tablaA'=>model->ID,);
$this->_tablaB->save();
$this->redirect(array('view','id'=>$model->iD));
}
$this->render('create',array( 'model'=>$model, ));
}

What I'm doing wrong? Any help is appreciated

You mean this?
Posted Image
0

#4 User is offline   sanat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 26-September 12
  • Location:Caracas, Venezuela

Posted 03 February 2013 - 10:37 AM

Thank you Softark, it worked. I only read the $_POST after the save and it works perfectly

Thank you Fouss.

Thank you Community
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