CakePHP Accessing Dynamically Created Tables?

Within the web application, users can upload data files that generate a new table in a separate MySQL database to store data. They can then manipulate this data in various ways.

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

I have a different database configuration and I can create tables when loading data as accurately as possible, but once that is done I cannot access the new table from the controller as part of the record's CRUD actions to manipulate the data.

I was hoping it would be down the line

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

//use data 
}

      

But it returns an error

Undefined property: ReportsController :: $ New_table

Fatal error: Calling member function find () on non-object in / app / controllers / reports _controller.php on line 60

Can anyone help.

+1


a source to share


2 answers


You need to call $this->loadModel('newTable')

to initialize it correctly. Cake needs to properly initialize $ this-> newTable and call all callbacks.



Of course you don't need one $this->uses[] = 'newTable';

, which does nothing but add another value to the $ uses array.

+1


a source


try:

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

      



If your table is called "new_tables" your model name should be "NewTable"

0


a source







All Articles