Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:57:32+00:00 2026-06-13T23:57:32+00:00

Ok, my first question was modified so many times that I chose to delete

  • 0

Ok, my first question was modified so many times that I chose to delete it and reformulate my question. I’ve made a small test-case project with different model-names to find a proper solution for my problem.

Warning: Don’t mix the databases up with tables

Motivation: I separated the user-data into multiple databases for legal & performance issues.

Currently I’m working on a CakePHP project that has multiple User‘s and each User has it’s own database with multiple tables (cars is one of the tables). Now, I need to explain something first:

Every User has his own database (not a table, a database), so the database names are as follows.

  • [DATABASE] app (This is the app’s main database)
    • [TABLE] users
    • [TABLE] permissions (Not relevant for this question)
  • [DATABASE] app_user1 (User.id 1 owns this entire database)
    • [TABLE] cars (a table entirely owned by User.id 1)
  • [DATABASE] app_user2 (User.id 2 owns this entire database)
    • [TABLE] cars (a table entirely owned by User.id 2)
  • etc…

I made a small drawing which might clarify the database / table -definitions and their relations to the models:

How to use multiple databases dynamically for one model in CakePHP

The problem!!!

I don’t know which database to connect to until the User logs in. User‘s and their databases are created dynamically, so I cant use app/Config/database.php.

So I’m currently writing an extension on the Model and ConnectionManager classes to bypass CakePHP’s basic database behaviors. So the Car model knows which database to use. But I just have a feeling this could be done easier!

So I guess it all boils down to one question:

Is there an easier way of doing this?!

Thanks to anyone who will take the time and effort of reading and understanding my problem!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:57:33+00:00Added an answer on June 13, 2026 at 11:57 pm

    This gentleman (Olivier) had the same problem! (A year ago) He wrote a small adaptation for the Controllers! It’s pretty small and it turns out, it works in 1.3 and 2.x.

    Anyhow, this is my final solution, that I put in the app/Model/AppModel.php:

    class AppModel extends Model
    {
      /**
       * Connects to specified database
       *
       * @param String name of different database to connect with.
       * @param String name of existing datasource
       * @return boolean true on success, false on failure
       * @access public
       */
        public function setDatabase($database, $datasource = 'default')
        {
          $nds = $datasource . '_' . $database;      
          $db  = &ConnectionManager::getDataSource($datasource);
    
          $db->setConfig(array(
            'name'       => $nds,
            'database'   => $database,
            'persistent' => false
          ));
    
          if ( $ds = ConnectionManager::create($nds, $db->config) ) {
            $this->useDbConfig  = $nds;
            $this->cacheQueries = false;
            return true;
          }
    
          return false;
        }
    }
    

    And here is how I used it in my app/Controller/CarsController.php:

    class CarsController extends AppController
    {
      public function index()
      {
        $this->Car->setDatabase('cake_sandbox_client3');
    
        $cars = $this->Car->find('all');
    
        $this->set('cars', $cars);
      }
    
    }
    

    I’m betting, I’m not the first or last one with this problem. So I really hope this information will find people & the CakePHP community.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First question here: it is a very short yet fundamental thing in Java that
First question from me; I'm currently fixing a graphing service that uses XSLFO to
First question here so hello everyone. The requirement I'm working on is a small
Note: Now that I know where the issue comes from, I modified the question.
First question on Stackoverflow (.Net 2.0): So I am trying to return an XML
First question is, given a url to an mp4 video, how can I save
(First question related to iPhone development, so apologies for sounding off-track.) I'm creating a
First question is, Am I on the right path?.ıs there a better way to
First Question in here :) (AS3) First off, how do I access a variable
My first question on codeigniter. I have integrated paypal in codeigniter using paypal library.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.