Note that there are some explanatory texts on larger screens.

plurals
  1. POChange default database in cakephp controller
    text
    copied!<p>Using CakePHP version : 2.3.1</p> <p>I have a default database that I use for authorisation, and based on that, different databases for different clients (database names have client names in it).</p> <p>I have many models with various relationships between them. I want to retrieve them using one call (which recursively retrieves all associated model data).</p> <p>Scenario: </p> <p>Databases</p> <pre><code>default : clients [id, password, name] [1, 'qwertycolemak', 'amazon'] [2, '5t4ck0verfl0w', 'ebay'] </code></pre> <p>Non-default databases(next 2)</p> <pre><code>client_amazon students[student_id, student_name] course_students [student_id, course_id] courses [course_id, course_name] client_ebay (same as client_amazon) </code></pre> <p>Now, suppose I get request for [id:2, password:'5t4ck0verfl0w'] I check default database (<i>clients</i>), check password, retrieve name, in this case <b>ebay</b></p> <p>Now , the database I want to access is <b>'client_ebay'</b></p> <p>I have different configs in database.php corresponding to each client. I tried changing datasource by using </p> <pre><code>$this-&gt;student-&gt;setDatasource('client_ebay') $this-&gt;course-&gt;setDatasource('client_ebay') $this-&gt;course_student-&gt;setDatasource('client_ebay') </code></pre> <p>This works for individual CRUD calls to models(non-recursive).</p> <p>But when I use a call (with recursion on) like </p> <pre><code>$this-&gt;student-&gt;findById(5) </code></pre> <p>datasource defaults to 'default', and I get an error :<br> <b>Table <i>students</i> for model <i>student</i> was not found in datasource <i>default</i></b></p> <p>How do I change the default datasource for all models(not one by one) dynamically through the controller?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload