Yii Framework Forum: Automatic Insert In Detale Table - Yii Framework Forum

Jump to content

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

Automatic Insert In Detale Table Rate Topic: -----

#1 User is offline   Reza Bagheri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 19-January 13
  • Location:Georgia

Posted 05 February 2013 - 11:27 PM

I have 2 table:
1- user:
  • id
  • username
  • password
  • ...


2- profile
  • id
  • user_id=>referenced to user->id
  • some access rules
  • ...



in User class I have
public function relations()
		{
		return array(
			'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
			);
		}



and in Profile class:

public function relations()
	{
	return array(
		'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}


I want when created new user, in profile table created one record with user_id reference.
so I do this:(in User class)

protected function afterSave()
		{
		parent::afterSave();
		$this->profile = new Profile;
		$this->profile->user_id = $this->id;
		$this->profile->save();		
		}	


Code run widthout any exception, but dont insert anything in profile table!
whats wrong?
0

#2 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 06 February 2013 - 12:33 AM

There may be a problem with Profile rules, check rules method,
by the way I think it's better to use $profile instead of $this->profile
0

#3 User is offline   fouss 

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

Posted 06 February 2013 - 06:51 AM

why not use a transaction in user create action function ?
Posted Image
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