Yii Framework Forum: Saving data from within the model - Yii Framework Forum

Jump to content

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

Saving data from within the model Rate Topic: -----

#1 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 04 October 2009 - 11:07 AM

I have a function inside my model which adds new records to a database. The problem is that, within the model itself you cannot save data using $this->save as it will try to update a record based on the provided fields unless you do something like this:

public function add ( $role_id, $user_id ) {
	$user_roles = new self ();
	$user_roles->user_id = $user_id;
	$user_roles->role_id = $role_id;
	if ( $user_roles->validate () ) {
		return $user_roles->save ();
	}
	return FALSE;
}


Please let me know if you know any other way around this.
0

#2 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 04 October 2009 - 11:39 AM

are you sure you really want to call $this->save() from within the model itself? i've never heard of this needing to be done
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 06 October 2009 - 05:01 AM

When you have such a method, you should call ClassName::model()->add(1,2).
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