Monday, January 4, 2010

How can I get an option/select drop down to populate with a table.myfield instead of the table.id (primary key).

I am a new baker and I am stuck in the dough. I am trying to insert a
option/select drop down but cannot get the field I need to show in the
drop down
I have the following tables:

CREATE TABLE IF NOT EXISTS `employees` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`state_id` int(11) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `employees.state_id` (`state_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


CREATE TABLE IF NOT EXISTS `states` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`state` varchar(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;

INSERT INTO `states` (`id`, `state`) VALUES
(1, 'AL'),
(2, 'AR'),
(3, 'AZ'),
(4, 'CA'),
(5, 'CO'),
(6, 'CT'),
(7, 'DC'),
(8, 'DE');

The following two models are generated through console tool cake bake
model using the default options provided.

class State extends AppModel {
var $name = 'State';
var $belongsTo = array(
'Employee' => array(
'className' => 'Employee',
'foreignKey' => 'state_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}

class Employee extends AppModel {
var $name = 'Employee';
var $belongsTo = array(
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}

I would like to show the "states.state" field but all I get is the
"states.id" field in the dropdown. Where have I gone wrong?

Any help is appreciated!

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: