This is just a very simple snippets to automate set “owned by user” attribute to the currently logged in user id upon creation of a record using Yii Framework.
Assuming a dataset is “owned” by a user, we need to set the id of the current logged in user when saving the dataset automatically. We will use beforeSave()
to set this attribute. This method is invoked before saving a record (after validation, if any).
public function beforeSave()
{
$this->user_id = Yii::app()->user->id;
return parent::beforeSave();
}
No Comments
Leave a comment Cancel