1. PHP
  2. Yii Framework

Yii Framework Automate setting the “owned by user” attribute to the currently logged in user

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();
}
Comments to: Yii Framework Automate setting the “owned by user” attribute to the currently logged in user

    Your email address will not be published. Required fields are marked *

    Attach images - Only PNG, JPG, JPEG and GIF are supported.