Saving data from within the model

(Manilodisan) #1

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.

(Poppitypop) #2

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

(Qiang Xue) #3

When you have such a method, you should call ClassName::model()->add(1,2).