Dean
Dean

Reputation: 1236

Creating a lookup field in a CakePHP form

I have a view created using Bake that has the 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 to the 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 is that when the form is displayed, it shows DeviceTypeID and UserID as well as UpdateID as the foreign key value instead of a drop down with the caption being the text and the value being the ID column. How would I go about setting a field from the foreign table to be the display field and the id as being the value?

Upvotes: 0

Views: 989

Answers (1)

Cris Sawfish
Cris Sawfish

Reputation: 335

Update 11-02-2013

First of all I strongly suggest to convert your primary and foreign keys accordingly so that they meet the CakePHP naming conventions.

This means that:

  • DeviceID should be id.
  • DeviceTypeID should be device_type_id
  • UserID should be user_id

Also all primary keys in your tables should be named as id. This way you will never have to worry about anything, concerning your models etc.

After that, all your tables must be in plural form. This means that device table should be devices, so you should rename it also. I assume that you also have the following tables: devices_types and users.

At this point, I should notice that I would prefer to have a table named devicetype. I avoid underscored names, because it's very easy to make mistakes using the correct form for each object, class etc. So I don't have to worry whether I should use the CamelCase or not.

Anyway

Your Device model should be something like 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 that meet your needs **/
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        )
    );
};
?>

Also your DeviceType model should be similar to

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

In your edit() method, you should query your DeviceType in something like this:

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

This way in your view the respective form element sets the <select> menu correctly.

PS: You should follow the CakePHP conventions about model-naming etc... Mine was just an example.

Upvotes: 2

Related Questions