Monday, October 4, 2010

Re: cakephp dynamic databases.

Like this?

App::import('Component', 'Session');
App::import('Core', 'ClassRegistry');
$userClass = ClassRegistry::init('User');

$Session = new SessionComponent();

if($Session->check('User')){
  $user = $userClass->findById($Session->read('User'));
 
  ConnectionManager::create(
          $name = 'user',
          $config = array('driver' => 'mysql',
                          'persistent' => false,
                          'host' => $user['User']['name'],
                          'login' => $user['User']['dbuser'],
                          'password' => $user['User']['dbpass'],
                          'database' => $user['User']['dbname'])
  );
}



2010/10/4 Jos Gerrits <josgerrits90@gmail.com>
:) thanks for helping me.

much apreciated.

2010/10/4 Dr. Loboto <drloboto@gmail.com>

Yes, this is something I mean.

Just one little suggestion - instantiate model as
ClassRegistry::init('User') instead of App::import() and new User().
As only in this case model will be fully initialized.

On Oct 4, 2:44 am, Jos Gerrits <josgerrit...@gmail.com> wrote:
> Finally figured it out.. YAAAAAAAAAAAY. (sorry, took me 3 days and the
> solution was pretty obvious and nowhere to be found..).
> Thanks for pointing the bootstrap thing out :) Your help was *awesome!*
>
> Thought i'll share it since it cannot be found on the internet after
> searching for 3 days.
>
> 1 Simple solution :)
>
> *Bootstrap.php*
>
> *App::import('Component', 'Session');
> App::import('Model', 'User');
> $SessionClass = new SessionComponent();
> $UserClass = new User();
>
> if($SessionClass->check('User')){
>   $user = $UserClass->findById($SessionClass->read('User'));
>
>   ConnectionManager::create(
>           $name = 'user',
>           $config = array('driver' => 'mysql',
>                           'persistent' => false,
>                           'host' => $user['User']['name'],
>                           'login' => $user['User']['dbuser'],
>                           'password' => $user['User']['dbpass'],
>                           'database' => $user['User']['dbname'])
>   );
>
> }*
>
> Once the user session is active (User is logged in) switch to the userDb.
> If needed you can still switch back. (just define the $useDbConfig in the
> model for when u need either the default or the user db.)
>
> Thanks again Dr. Loboto :)
>
> 2010/10/2 Dr. Loboto <drlob...@gmail.com>
>
>
>
> > You can improve your beforeFilter solution by doing it in
> > bootstrap.php: request login database there and create new database
> > config. In this case you can keep $useDbConfig in all models pointing
> > to dynamic config and do not hit missing DB error.
>
> > On Oct 1, 7:52 pm, Jos Gerrits <josgerrit...@gmail.com> wrote:
> > > Thats whats happening,
>
> > > i have 1 database which has the login details for the other databases.
>
> > > 2010/10/1 Jeremy Burns | Class Outfit <jeremybu...@classoutfit.com>
>
> > > > Couldn't store this data in a separate database that just has log in
> > > > details?
>
> > > > Jeremy Burns
> > > > *Class Outfit*
> > > > *
> > > > *
> > > > jeremybu...@classoutfit.com <jeremybu...@mac.com>
> > > >http://www.classoutfit.com
>
> > > > On 30 Sep 2010, at 20:06, nithiz wrote:
>
> > > > Hi all,
>
> > > > im developing an application which has the cakephp installed on 1
> > > > server and has a login system for customers to connect to their own
> > > > databases.
>
> > > > Now my question is, is there an alternative for manualy entering all
> > > > customers (1000+) in the database.php config file?
>
> > > > I have all my customers in a local database with all their database
> > > > configurations. So my default in my database.php config in cakephp is
> > > > the local database with all my customers.
>
> > > > I have done this one time before by having my app_controller do a
> > > > beforeFilter() and retrieve the database configurations from my local
> > > > database depending on a login + pass. (although, this caused problems
> > > > every time..)
>
> > > > Thanks in advance :)
>
> > > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp
> > others
> > > > with their CakePHP related questions.
>
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "CakePHP" group.
> > > > To post to this group, send email to cake-php@googlegroups.com
> > > > To unsubscribe from this group, send email to
> > > > cake-php+unsubscribe@googlegroups.com<cake-php%2Bunsubscribe@googlegroups.c om>For more options, visit this group
> > > > athttp://groups.google.com/group/cake-php?hl=en
>
> > > >  Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp
> > > > others with their CakePHP related questions.
>
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "CakePHP" group.
> > > > To post to this group, send email to cake-php@googlegroups.com
> > > > To unsubscribe from this group, send email to
> > > > cake-php+unsubscribe@googlegroups.com<cake-php%2Bunsubscribe@googlegroups.c om>
> > <cake-php%2Bunsubscr...@googlegroups.c om>For more options, visit this
> > group at
> > > >http://groups.google.com/group/cake-php?hl=en
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organd help others
> > with their CakePHP related questions.
>
> > You received this message because you are subscribed to the Google Groups
> > "CakePHP" group.
> > To post to this group, send email to cake-php@googlegroups.com
> > To unsubscribe from this group, send email to
> > cake-php+unsubscribe@googlegroups.com<cake-php%2Bunsubscribe@googlegroups.c om>For more options, visit this group at
> >http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en


Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: