Thanks a lot for the answer! That kind of database selection is indeed
perfect for me and much simpler.. don't have to do any custom db
"drivers". Thanks again!

I have couple questions though, as this is the first time I'm using
CakePHP and don't know much about it yet.

> > 1. Can I link tables between database configs?
>
> Yes, but Cake will always run separate queries, no any joins.

How do you define a link between Models that are not in the same
database? I know that with useDbConfig I can tell CakePHP that which
database to use for the model, but when I'm creating a link from users
to employees table for example, how can I tell CakePHP that the
employees table is in the other database? Or do I need to tell that
even? Will Cake check the useDbConfig from the employee model
automatically?

E.g. would this simply work:

class User extends AppModel {
        var $useDbConfig = 'commondb';
        var $name = 'User';
        var $hasOne = 'Employee';
}

class Employee extends AppModel {
        var $useDbConfig = 'companydb';
        var $name = 'Employee';
}

// get employee object from the user object
$employee = $user->Employee;

Then to another question...

> You can determine and save database name by something like this (I
> assume that routing with param 'company' is used):

I tried to read about the routing in CakePHP, but I didn't quite get
it.. so how can I make it work with urls like:

www.domain.com/company1/controller/action
www.domain.com/company2/controller/action

and take that first part of the url out to save as db name (with
Configure::write like you pointed out) and then use the rest of the
url to navigate normally to the controller / action. I could do it in
the app/webroot/index.php before the $url parameter is passed to the
dispatcher, but I'm guessing that would be the wrong way to do it. Can
I do it with the routing somehow?

These might be quite trivial things for more advanced CakePHP users,
but for me as a newbie, I haven't quite grasped the idea yet..

I'm definitely going to do some write up of the whole setup.


On May 15, 5:44 am, "Dr. Loboto" <[email protected]> wrote:
> > 1. Can I link tables between database configs?
>
> Yes, but Cake will always run separate queries, no any joins.
>
> > 2. How to automate the database table selection?
>
> Method on link you provided looks too complex for your situation. If
> each company have own databases with completely same structure, the
> only thing you need to do for company specific models is to select
> appropriate DB config.
>
> You can determine and save database name by something like this (I
> assume that routing with param 'company' is used):
>
> class AppController extends Controller {
>         function constructClasses() {
>                 // Save company name when we already know it but before 
> models are
> created
>                 Configure::write(
>                         'companyDB',
>                         !empty($this->params['company']) ? 
> $this->params['company'] : false
>                 );
>                 parent::constructClasses();
>         }
>
> }
>
> Mark all company-specific models like this:
>
> class CompanyProfile extends AppModel {
>         var $specific = true;
>
> }
>
> And have such AppModel:
>
> class AppModel extends Model {
>         var $specific = false;
>
>         function __construct($id = false, $table = null, $ds = null) {
>                 if ($this->specific) {
>                         // Get saved company/database name
>                         $dbName = Configure::read('companyDB');
>                         // Get common company-specific config (should be 
> present in
> database.php)
>                         $config = 
> ConnectionManager::getDataSource('defaultSpecific')->config;
>
>                         // Set correct database name
>                         $config['database'] = $dbName;
>                         // Add new config to registry
>                         ConnectionManager::create($dbName, $config);
>                         // Point model to new config
>                         $this->useDbConfig = $dbName;
>                 }
>                 parent::__construct($id, $table, $ds);
>         }
>
> }
> > ... So I'd like to use the first part of the url instead.
>
> Read about routing:http://book.cakephp.org/view/46/Routes-Configuration
>
> On May 15, 2:13 am, doze <[email protected]> wrote:
>
> > Hello,
>
> > I would like to get an advice on how to build an application with
> > CakePHP where there are multiple customers (companies) using the
> > application and each have their own database, but also some common
> > tables in another database. Let me explain the database structure:
>
> > Tables in common database:
>
> > - Users
> > - Companies
> > - Common settings
> > - ...
>
> > Tables in company specific database:
>
> > - Users extra profile data
> > - Company employees
> > - ...
>
> > So all users register to the application and the basic information is
> > stored in a table in the common database. The application lists
> > companies that are available. When users goes to some specific company
> > page for the first time, they need to add extra profile data that the
> > company wants. This data is stored to the company database.
>
> > So I have couple questions.
>
> > 1. Can I link tables between database configs?
>
> > E.g. if you look at the tables above, the users extra profile data
> > table in company database have user_id foreign key what is the ID of
> > the user in Users table in common database. Same goes for the company
> > employees table. It has a user_id foreign key, all employees are just
> > some users with extra fields in that table and employee id.
>
> > 2. How to automate the database table selection?
>
> > I briefly checked this 
> > article:http://recurser.com/articles/2007/06/04/multiple-dbs-in-cakephp/
>
> > That seems to work for me also to some extent. I can adapt that
> > information and then have two database configurations in app/config/
> > database.php. One config for the common database that never changes
> > and then another config what uses the system from the above article to
> > make the database selection. Does someone have better idea?
>
> > But the more important question for the database table selection is
> > that how to know what database to select. The article selects the
> > database via user info, but in my application, users are not tied to
> > some specific database. I was going to do it with subdomains. The
> > application would check what subdomain is used and then find the
> > database for that subdomain. It would work all ok, but I need to build
> > an installation system to the application what can be used to add
> > companies easily online. So I'd need to make it add the subdomain also
> > and vhost to apache and restart apache. I don't like the idea of
> > having to restart apache every time a company is added. So I'd like to
> > use the first part of the url instead. There would be urls like this:
>
> >www.domain.com/company1/homewww.domain.com/company2/home
> > ....
>
> > So what I'm thinking (without knowing CakePHP capabilities) is to hack
> > app/webroot/index.php and take the company part out from the $url and
> > store it to session and read it from there when it's time to select
> > the database. That way the application would select the correct
> > database based on the url. But it requires modifications to the app/
> > webroot/index.php and there might be a better way to do this. So is
> > there? :)
>
> > Could I for example extend the CakePHP dispatcher what would take the
> > first part from the url out and store it somewhere and then use the
> > rest of the url normally to navigate to the controller etc? or...?
>
> > Thanks in advance if you took the time to read all this :) Sorry for
> > the lengthy post.
>
> > Oh and environment is:
>
> > Apache HTTPD 2.2.11
> > MySQL 5.1.33
> > PHP 5.2.9
> > CakePHP 1.2.3.8166

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to