CakePHP show other field than id in dropdown-lists
I got two tables: users and firms:
class Firm extends AppModel {
var $name = 'Firm';
var $hasAndBelongsToMany = 'User';
}
and:
class User extends AppModel {
var $name = 'User';
var $hasMany ='Post';
var $hasAndBelongsToMany = 'Firm';
}
I use scaffold var to display all save/view/edit etc methods. I override the add method like this:
function add(){
if (!empty($this->data)) {
$this->User->create();
$this->User->save($this->data);
开发者_C百科 $this->redirect(array('action'=>'index'), null, true);
}
$firms = $this->User->Firm->find('list');
$this->set('firms', $firms);
}
Everything works great, but when i use users/add, I got dropdown with id: "1","2",etc.
I would like to display Firm name, not id. How to do that?You need to set the 'displayField' property in your models, this is what Cake uses in various built in functions, including find('list');` This defaults to 'name' or 'title' field in your database, but I'm guessing you don't have those fields in the firms database.
Change it with this in your Firms model:
var $displayField = 'firm_name';
精彩评论