Cakephp: Load models without model files
I have tables that are created on the fly and I'd like to access it thru Model. Since those tables are dy开发者_JS百科namically created, I don't have the model .php files for them. The tables are created conforming to Cake's naming convention. I tried to use loadModel() to load them but Cake still complains that the tables are missing.
Is there a solution for it?
Thank you.
As of this writing, using CakePHP 2.1.2
In your controller add App::uses('AppModel', 'Model');
. Then instantiate the model
$RemoteHeader = new AppModel(array(
'table' => 'SOP10100',
'ds' => 'external',
'name' => 'RemoteHeader',
'alias' => 'InvoiceHeader',
'primaryKey' => 'id',
'hasMany' => array(
'InvoiceDetail' => array(
'foreignKey' => 'SOPNUMBE',
'counterCache' => false
))));
then fetch the data with any Model parameters you need:
$data = $RemoteHeader->find('all', array(
'fields' => array(
'SOPNUMBE',
'CUSTNAME',
'DUEDATE',
'DOCDATE',
'DOCAMNT',
'SUBTOTAL')));
Create /app/models/dynamic.php
with class
class Dynamic extends AppModel {
var $name = 'Dynamic'
var $useTable = null;
}
in Your controller's action
$this->Dynamic->useTable = 'newly-created-table';
It's not tested, let me know if having issues
精彩评论