Yii Framework Forum: How To Save Data Into A Database Table. - Yii Framework Forum

Jump to content

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

How To Save Data Into A Database Table. Rate Topic: -----

#1 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 05 December 2012 - 01:29 AM

Hi All,

I have two tables, table_a and tabl_b.
after saving data in table_a, i want to save some columns of table_a those are currently saved into a database table of table_b.


any help plz...


Thanks
0

#2 User is offline   Keith 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 868
  • Joined: 04-March 10
  • Location:UK

Posted 05 December 2012 - 04:12 AM

What have you tried so far?
0

#3 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 05 December 2012 - 04:33 AM

View PostKeith, on 05 December 2012 - 04:12 AM, said:

What have you tried so far?

I tried this code for inserting values from model a to model b.
 protected function afterSave()
{
	$clientid = Client::model()->findByPk(Yii::app()->user->id);
	Packageassignment::model()->user_id = $clientid;
	Packageassignment::model()->package_id= $this-->package_id;
	Packageassignment::model()->package_start_date =$this->p_start_date;
	
	
   Packageassignment::model()->package_end_date   =$this->p_end_date;

	$clientid->Save(false);
   
}

0

#4 User is offline   Keith 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 868
  • Joined: 04-March 10
  • Location:UK

Posted 05 December 2012 - 05:13 AM

And are you trying to create a new record in B or trying to update an existing one?

Note that at the moment, assuming the afterSave() method is in the class of $a, it will be called in an infinite loop as you're saving $a within it's own afterSave() method.
0

#5 User is offline   lifo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 12-November 12

Posted 05 December 2012 - 05:22 AM

i have tried like below and its working for me::

protected function afterSave() { // "afterSave" of Post model
	parent::afterSave();
				
	$comment= Comment::model()->findByPk(7);
	$comment->content=$this->name;
        $comment->description='copied from post';		
	$comment->save(false);
}

0

#6 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 05 December 2012 - 06:03 AM

View PostKeith, on 05 December 2012 - 05:13 AM, said:

And are you trying to create a new record in B or trying to update an existing one?

Note that at the moment, assuming the afterSave() method is in the class of $a, it will be called in an infinite loop as you're saving $a within it's own afterSave() method.


@Keith..plz see my updates..i just want to copy a record from current model to a database table.thanks
0

#7 User is offline   fouss 

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

Posted 06 December 2012 - 03:21 AM

View Posthameedhamdani, on 05 December 2012 - 06:03 AM, said:

@Keith..plz see my updates..i just want to copy a record from current model to a database table.thanks


When do you need to make this copy? you may not need function afterSave() ...........
Posted Image
1

#8 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 06 December 2012 - 03:46 AM

View Postfouss, on 06 December 2012 - 03:21 AM, said:

When do you need to make this copy? you may not need function afterSave() ...........


i need copy data after saving in database.i want only specific data to be copied into other database tables.

thanks
0

#9 User is offline   fouss 

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

Posted 06 December 2012 - 03:57 AM

View Posthameedhamdani, on 06 December 2012 - 03:46 AM, said:

i need copy data after saving in database.i want only specific data to be copied into other database tables.

thanks


ok!

protected function afterSave() //Copy data from Client to packageassignment *** assuming your you're in the Client model 
								//and Clien has id,package_id,p_start_date
{
	$packageassignment= new Packageassignment();
	$packageassignment->user_id=$this->id;
	$packageassignment->package_id=$this->package_id;
	$packageassignment->package_start_date=$this->p_start_date;
	$packageassignment->save();
	return parent::afterSave();
}


Posted Image
1

#10 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 06 December 2012 - 01:26 PM

View Postfouss, on 06 December 2012 - 03:57 AM, said:

ok!

protected function afterSave() //Copy data from Client to packageassignment *** assuming your you're in the Client model 
								//and Clien has id,package_id,p_start_date
{
	$packageassignment= new Packageassignment();
	$packageassignment->user_id=$this->id;
	$packageassignment->package_id=$this->package_id;
	$packageassignment->package_start_date=$this->p_start_date;
	$packageassignment->save();
	return parent::afterSave();
}



Sir No record is copy from client to packageassignment.plz advise.thanks
0

#11 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 08 December 2012 - 05:51 AM

View Postfouss, on 06 December 2012 - 03:57 AM, said:

ok!

protected function afterSave() //Copy data from Client to packageassignment *** assuming your you're in the Client model 
								//and Clien has id,package_id,p_start_date
{
	$packageassignment= new Packageassignment();
	$packageassignment->user_id=$this->id;
	$packageassignment->package_id=$this->package_id;
	$packageassignment->package_start_date=$this->p_start_date;
	$packageassignment->save();
	return parent::afterSave();
}




@fouss...sir no data is copying into 2nd table...plz advise..thanks
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