Saturday, December 31, 2011

Re: Multiple Businesses in one database

I do a very similar thing to allow users multiple profiles. So they can segregate their data by company, or family, or team.

Here is the basic logic.

In the appController beforeFilter() method set the logged in user to the appModel class that you are fetching. Such as...
AppModel::setActiveUser($this->Auth->user());

Of course you will need to set up a static property in the AppModel. Such as...
public static $activeUser;
public static function setActiveUser($appUser){
self::$activeUser = $appUser;
}

Once you do this, any model that is based on multi-business logic, will have access to the current logged in user.

Now, in your Events model, you could add the following code... (or something similar)
function beforeFind($qd){
if(isset(parent::$activeUser['User']['business_id'])){
$qd['conditions'][] = array(
'Event.business_id '=> parent::$activeUser['User']['business_id']
 );
}
return $qd;
}


If you want to do a much simpler method...
You can simply add a condition to the paginate options in the controller index method.
$appUser = $this->Auth->user();
$this->paginate['conditions'][] = array('Event.business_id' => $appUser['User']['business_id']);

Happy Coding!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: