definition in your State model looks wrong. Shouldn't that be a
$hasMany definition? That means that an Employee belongs to a State,
and a State has many Employees.
The drop down is displayed in a view and the contents of the drop down
are populated in the controller action that calls the view.
In the function (probably edit or add, I would guess) add these lines:
$states = $this->Employee->State->find('list');
$this->set(compact('states'));
compact can take more than one variable. So if you end up with more
than one drop down repeat the first line and add the new variable
(minus the $ sign) to the compact array, e.g. $this->set(compact
('states', 'countries'));
In your view, your drop down is created like this:
echo $form->input('state_id', array('empty' => true));
Cake will use the contents of the $state array to populate the drop
down, displaying the name of the state. The empty option means that
nothing is selected by default. Without it, your form will display the
first entry and assume that the user wants to select that one.
Hope that helps.
On Jan 4, 10:36 pm, Guran <guran.banga...@gmail.com> wrote:
> 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:
Post a Comment