Saas Using Mysql Multi Tenant Strategy For Saas Using Mysql

Saas Using Mysql - multi tenant strategy for saas using mysql

How we create a SaaS app using Yii?

can anything do with extends CActiveRecord ??

i want to read the data of the current db user only…im saving the current username by a trigger …

any simple method?

any idea?




class Content extends CActiveRecord

{

    public function defaultScope()

    {

        return array(

            'condition'=>"language='".Yii::app()->language."'",

        );

    }

}



IN the code below, i am using tenant_id to separate the records.

beforeSave() method in model to populate with current username. Example:


	public function beforeSave()

	{

		if($this->isNewRecord)

		{

			$this->user_created=Yii::app()->user->name;

			$this->tenant_id = Yii::app()->user->tenantId;

			$this->project_id = Yii::app()->user->projectId;

		}

		else

		{	

			$this->date_updated=new CDbExpression('NOW()');

			$this->user_updated=Yii::app()->user->name;

		}

		return true;

	}

and defaultScope() method to always retrieve record with current username. ex:


      public function defaultScope()

	{

		return array(

			'condition'=>'tenant_id='.Yii::App()->user->tenantId,

                        'order'=>'change_request_status_id ASC, name DESC'

		);

	}

so which is better approach override or change the CActiveRecord ?

change CActiveRecord like this or ??




protected function beforeSave()

	{

                $this->tenant_id = Yii::app()->user->tenantId;


		if($this->hasEventHandler('onBeforeSave'))

		{

			$event=new CModelEvent($this);

			$this->onBeforeSave($event);

			return $event->isValid;

		}

		else

			return true;

	}



and change CActiveRecord




public function defaultScope()

        {

                return array(

                        'condition'=>'tenant_id='.Yii::App()->user->tenantId,

                        

                );

        }



is this ok?