In CAKEPHP, can we dynamically change the table associated with a specific model?

Suppose I have one identical table that has the same structure (name it "tableA" and "tableB").

I want to save certain data in table "A" and some data in table "B".

NOW I want to use the same model for both tables.

I want to modify the table associated with the model (for example, "ModelM") to dynamically change depending on the state on the controller.

eg.


In the controller: // sample code

function saveProduct(){ $this->loadModel('ModelM'); if(condition){ $this->ModelM->useTable = 'A'; }else{ $this->ModelM->useTable = 'B'; } $this->ModelM->save($this->data); } 

APPENDIX JANUARY 14, 2011

Below is a copy / paste of the code I'm working on:

 function experiment(){ $tableName = 'temp_table'.'1234'; mysql_query('CREATE TABLE '.$tableName.' LIKE temp_home_masters'); $sql = $this->createInsertQuery($new_arr,$tableName); $status = mysql_query($sql); if($status){ echo "saved successfully"; }else{ echo "error"; } $this->NewHomeMaster->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->paginate('NewHomeMaster',array($new_conditions)); mysql_query('DROP table '.$tableName); } 

IT DOES NOT WORK DIRECTLY ...

+6
php cakephp dynamic-data model
source share
3 answers

I initially decided to understand the solution to your problem, but the more I think about it, I think your logic is corrupted.

I see how the fact that the tables are similar or identical can lead to a decision to use the same model to interact with both tables. However, when you look at what a model should be (CakePHP basically has a table interface), it makes no sense to switch back and forth.

CakePHP docs explain the models as follows:

In object-oriented programming, a data model is an object that represents a “thing”, such as a car, person, or home.

In your example, you really have two separate “things” that look exactly the same. Therefore, they must have their own models.

If your models really have the same methods, then the “CakePHP path” will define a custom Behavior that encapsulates your common methods. Then attach the behavior to both models.

Then you can load the desired model in the controller state:

 private $DynamicModel; public function saveProduct() { if (condition) { App::import('Model', 'ModelZ'); $this->DynamicModel = new ModelZ; } else { App::import('Model', 'ModelY'); $this->DynamicModel = new ModelY; } $this->DynamicModel->save($this->data); } 
+3
source share

The situation is complicated, as Stephen describes, because your approach somewhat violates the MVC conventions.

However, if you are willing to go to the dark side of custom hacks, you might consider creating your own custom data source in CakePHP that handles this kind of logic for you. The option is to expand this data source (presumably in MySQL) with its own user logic, the purpose of which is to perform some preliminary filtering / conditioning before interacting with the database. Not so clean, because the logic is in the wrong volume, but it can work. Look here for a start: http://book.cakephp.org/view/1075/DataSources

Alternatively, you can create two different models and force them to use the same logic using behavior. This allows you to limit the choice of the model earlier in the stream (and, therefore, not only affects the location of the data warehouse), but it can also be an opportunity.

0
source share

Do this on your controllers before the filter function:

$ this-> CakePHPModelName-> setSource ('table_name');

This will use a different table.

Source: http://www.mainelydesign.com/blog/view/changing-cakephps-model-usetable-on-fly

0
source share

All Articles