Colt Infinity
Colt Infinity

Reputation: 5

Cakephp 2.0 Dropdown Select

After reading and applying all those suggested answer about dropdown, still i got NO RESULTS in my dropdown. im newbie and it really gives me headache solving this problem. I have client table associated with belongsTO Client_Group Table. Whatever code modifications i made up to naming convention, i cant still display the CLIENT GROUP' data to my dropdown list. Please HElp! Please help! thanks in advance

CREATE TABLE `clients` (
  `id` int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `client_group_id` int ,
  `client_package_id` int ,
  `client_account_id` int ,
  `name` VARCHAR(40), 

CREATE TABLE `client_groups` (
  `id` INTEGER NOT NULL AUTO_INCREMENT  PRIMARY KEY,
  `name` VARCHAR(50), 

    INSERT INTO `client_groups` (`id`,`name`) VALUES (1,'Top Company Holdings');
    INSERT INTO `client_groups` (`id`,`name`) VALUES (2,'Cadiz Group of Companies');

In CLient Model:

    public $belongsTo = array(
         'ClientGroup' => array(
            'className'    => 'ClientGroup',
            'foreignKey'   => 'client_group_id' ,
            'fields'       =>   'name'
             ),

Client Group Model:
     public $hasMany = array(      
        'Client' => array(
            'className'     => 'Client',
            'foreignKey'    => 'client_group_id',
            'order'         => 'Client.name DESC' )

In Clients Controller: $clientgroups= $this->Client->ClientGroup->find('list',array( 'type'=>'select', 'fields'=> array('id', 'name'), 'order' => array('name' => 'ASC'))); $this->set(compact('clientgroups'));

In Client Add.ctp :
    <?php echo $this->Form->input('client_group_id',array( 'option'=>$clientgroups ,  
               'type'=>'select', 'empty'=>'Select Group'));
    ?>

Upvotes: 0

Views: 1115

Answers (3)

Naveen Kumar
Naveen Kumar

Reputation: 465

you have client table associated with belongsTO Client_Group Table. you want to display the CLIENT GROUP' data to my dropdown list.

$clientGroup = $this->CLient Model->Client Group Model->find('list'); $this->set(compact('clientGroup'));

and in your ctp page do it this

echo $this->Form->input('client_group_id');

Upvotes: 2

William Espindola
William Espindola

Reputation: 29

Their convention this crazy!! Try to work this way, ok? ;)

CREATE TABLE `clients` (
  `id` int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `group_id` int ,
  `package_id` int ,
  `account_id` int ,
  `name` VARCHAR(40)
)

CREATE TABLE `groups` (
  `id` INTEGER NOT NULL AUTO_INCREMENT  PRIMARY KEY,
  `name` VARCHAR(50)
)

INSERT INTO `groups` (`id`,`name`) VALUES (
 1,'Top Company Holdings'
);
INSERT INTO `groups` (`id`,`name`) VALUES (
 2,'Cadiz Group of Companies'
);

In CLient Model:

public $belongsTo = array(
     'Group' => array(
        'className'    => 'Group',
        'foreignKey'   => 'group_id'
     )
);

In your Cliente controller

public function myFunction()
{
...
$groups = $this->Cliente->Group->find('list');
$this->set(compact('groups'));
...
}

In your view
echo $this->Form->input('group_id');

Upvotes: 0

Dave
Dave

Reputation: 29121

Have you tried setting the variable using camel case? $clientGroups and 'clientGroups'? That's the correct naming convention.

Upvotes: 1

Related Questions