Note that there are some explanatory texts on larger screens.

plurals
  1. POIn CAKEPHP can we dynamically change the table linked to a particular model?
    text
    copied!<p>Suppose I have 2 identical table having same structure(Call it 'tableA' &amp; 'tableB').</p> <p>I want to save certain data on table 'A' and certain data on table 'B'.</p> <p>NOW I want to use the same MODEL for both the table. </p> <p>I want to change the table linked with the Model(say 'ModelM') to change dynamically based on condition at the controller. </p> <p>e.g.</p> <hr> <p><strong>In controller:-</strong> //sample code</p> <pre><code>function saveProduct(){ $this-&gt;loadModel('ModelM'); if(condition){ $this-&gt;ModelM-&gt;useTable = 'A'; }else{ $this-&gt;ModelM-&gt;useTable = 'B'; } $this-&gt;ModelM-&gt;save($this-&gt;data); } </code></pre> <hr> <p><strong>ADDITION ON 14th JANUARY 2011</strong></p> <p>Following is the copy/paste of code I am working on:</p> <pre><code>function experiment(){ $tableName = 'temp_table'.'1234'; mysql_query('CREATE TABLE '.$tableName.' LIKE temp_home_masters'); $sql = $this-&gt;createInsertQuery($new_arr,$tableName); $status = mysql_query($sql); if($status){ echo "saved successfully"; }else{ echo "error"; } $this-&gt;NewHomeMaster-&gt;setSource($tableName);//NewHomeMaster was previously attached to a different table , here I want to change the tableName the model linked with dynamically.Model 'NewHomeMaster' already exists and uses a table ...Here I am willing to link this model to the newly created tempory table.// $home_details=$this-&gt;paginate('NewHomeMaster',array($new_conditions)); mysql_query('DROP table '.$tableName); } </code></pre> <p>UNFORTUNATELY THIS DOES NOT WORK...</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