views:

617

answers:

3

As part of a web application users can upload files of data, which generates a new table in a dedicated MySQL database to store the data in. They can then manipulate this data in various ways.

The next version of this app is being written in CakePHP, and at the moment I can't figure out how to dynamically assign these tables at runtime.

I have the different database config's set up and can create the tables on data upload just fine, but once this is completed I cannot access the new table from the controller as part of the record CRUD actions for the data manipulate.

I hoped that it would be along the lines of

function controllerAction(){ 
$this->uses[] = 'newTable'; 
$data = $this->newTable->find('all'); 

//use data 
}

But it returns the error

Undefined property: ReportsController::$newTable

Fatal error: Call to a member function find() on a non-object in /app/controllers/reports_controller.php on line 60

Can anyone help.

+1  A: 

You need to call $this->loadModel('newTable') to initialize it properly. Cake needs to initialize $this->newTable properly, and call all the callbacks.

Of course, you don't need $this->uses[] = 'newTable';, that doesn't do anything except add another value to the $uses array.

dr Hannibal Lecter
Cool. That's definitely a step in the right direction. Thanks.The only issue is that this only loads the model if the data table is in the app database, but these dynamic tables are created in their own database so I need to change the useDbConfig of the model somehow... any ideas?
Dave
Found it. Thanks for posting, I looked up the API for loadModel() and found ClassRegistry (which is what it calls), that can be passed an array including which datastore to use.
Dave
$this->newTable->useDbConfig = 'otherDatabaseConfigVar';
neilcrookes
Final Solution:$this->$dataTable = ClassRegistry::init(array('class'=>'newTable', 'ds'=>'dataDB'))$this->newTable ->find('all');
Dave
A: 

try:

function controllerAction() {
  $data = ClassRegistry::init('ModelNameForNewTable')->find('all');
}

If your table is called 'new_tables', your model name should be 'NewTable'

neilcrookes
Thanks for your help also. :) Got it working now
Dave
A: 

I'm trying to do the same thing, but am not having any luck instantiating the model via ClassRegistry::init(). I've traced the issue to creating the table and then instantiating the model within the same page request. There must be a process during Cake's startup routine (i.e. dispatcher) which reads the currently available tables from the database. Since I'm dynamically creating the table at runtime, and attempting to access it all within the same page request, Cake has no knowledge of this table.

I am hunting for the "load tables" routine that executes during dispatch, and will reply here if I find anything. If anyone has input on this, it's much appreciated! I thought this issue relevant to the thread title so I responded instead of creating a new question.

Kevin DeCapite