I am trying to load a generic Model in CakePHP.
What I am trying to achieve now is to load the history table and print it with its displayField and a html-link to that object.
Example-Data:
Table: History
id, model, entity_id
1 , User, 123
2 , Files, 345
Table: Users
id, name
123, 'Steve'
Table: Files
id, filename, filesize
345, 'test.txt', 666
When I print my history now I need something like this:
<tr>
<td>1</td> //id of history
<td><a href="/users/123">Steve</a></td> //link to user
<tr>
<td>2</td>
<td><a href="/files/345">test.txt</a></td> //link to file
<tr>
User should show the "name" and Files should show the "filename". Thats why I would like to use the displayField I set in the Model.
I thought I might try it with this code:
$this->loadModel('Users');
$this->loadModel('Files');
foreach($historyEntries as $entry){
$genericModel = $this->$entry['HistoryEntry']['model']->find('all');
}
But this code tells me that a 'Users-Helper' is missing. Any ideas how to get this to work?
Cheers!
Your model name should be User
, not Users
. (Same for Files
)
Change to
$this->loadModel('User');
Sure. This is pretty much all the code I have:
//Files Model
App::uses('AppModel', 'Model');
class Files extends AppModel {
public $displayField = 'filename';
}
//Users Model
App::uses('AppModel', 'Model');
class Files extends AppModel {
public $displayField = 'name';
}
//History Model
App::uses('AppModel', 'Model');
class History extends AppModel {
}
//History Controller
public function index(){
$this->loadModel('Users');
$this->loadModel('Files');
$historyEntries = $this->History->find('all');
$result = array();
foreach($historyEntries as $entry){
//1. How do I load the generic Model?
$genericModel = $this->$entry['History']['model']->find('first', array('conditions' => array('Article.id' => $entry['History']['entity_id'])));
//2. How do I access the DisplayField of this generic Model
array_push($result,$genericModel['displayField']);
}
$this->set('historyEntries',$result);
}