php - Creating a lookup field in a CakePHP form -



php - Creating a lookup field in a CakePHP form -

i have view created using bake has following:

<fieldset> <legend><?php echo __('edit device'); ?></legend> <?php echo $this->form->input('deviceid'); echo $this->form->input('devicetypeid'); echo $this->form->input('userid'); echo $this->form->input('type'); echo $this->form->input('keypadid'); echo $this->form->input('version'); echo $this->form->input('description'); echo $this->form->input('updateid'); ?> </fieldset>

which saves table:

create table `device` ( `deviceid` varchar(255) not null , `devicetypeid` int(11) not null , `userid` int(10) not null , `type` varchar(10) null default null , `keypadid` int(10) null default null , `version` varchar(255) null default null , `description` tinyblob null , `updateid` int(11) null default null , primary key (`deviceid`), index `fk_user` (`userid`), index `fk_devices_updates` (`updateid`), index `fk_device_devicetype` (`devicetypeid`), constraint `fk_device_devicetype` foreign key (`devicetypeid`) references `devicetype` (`devicetypeid`), constraint `fk_devices_updates` foreign key (`updateid`) references `update` (`id`) on update cascade on delete cascade, constraint `fk_user` foreign key (`userid`) references `user` (`userid`) on update cascade on delete cascade )

my problem when form displayed, shows devicetypeid , userid updateid foreign key value instead of drop downwards caption beingness text , value beingness id column. how go setting field foreign table display field , id beingness value?

update 11-02-2013

first of suggest convert primary , foreign keys accordingly meet cakephp naming conventions.

this means that:

deviceid should id. devicetypeid should device_type_id userid should user_id

also primary keys in tables should named id. way never have worry anything, concerning models etc.

after that, tables must in plural form. means device table should devices, should rename also. assume have next tables: devices_types , users.

at point, should notice prefer have table named devicetype. avoid underscored names, because it's easy create mistakes using right form each object, class etc. don't have worry whether should utilize camelcase or not.

anyway

your device model should that:

<?php /** device.php **/ class device extends appmodel { public $name = 'device'; public $belongsto = array( 'devicetype' => array( 'classname' => 'devicetype', 'foreignkey' => 'device_type_id' /** specify other keys meet needs **/ ), 'user' => array( 'classname' => 'user', 'foreignkey' => 'user_id' ) ); }; ?>

also devicetype model should similar to

<?php /** devicetype.php **/ class devicetype extends appmodel { public $name = 'devicetype'; };

in edit() method, should query devicetype in this:

... $devicetypes = $this->device->devicetype->find('list', array('id', 'caption')); $this->set(compact('devicetypes')); ...

this way in view respective form element sets <select> menu correctly.

ps: should follow cakephp conventions model-naming etc... mine example.

php cakephp cakephp-2.1

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -