Cakephp 2.0下拉选择

时间:2013-05-19 00:51:26

标签: list cakephp drop-down-menu

在阅读并应用所有关于下拉列表的建议答案后,我的下拉列表仍然没有结果。即时新手,这真的让我头痛解决这个问题。我有与belongsTO Client_Group表关联的客户端表。 无论我对命名约定做了什么代码修改,我仍然无法将CLIENT GROUP的数据显示在我的下拉列表中。请帮忙!请帮忙!提前谢谢

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' )

在客户端控制器中:          $ clientgroups = $ this-> Client-> ClientGroup-> find('list',array('type'=>'select',                 '字段'=>数组('id','name'),                 'order'=> array('name'=>'ASC')));           $这 - >设置(紧凑( 'clientgroups'));

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

3 个答案:

答案 0 :(得分:2)

您有与belongsTO Client_Group表关联的客户端表。你想显示客户群&#39;数据到我的下拉列表。

$ clientGroup = $ this-&gt; CLient Model-&gt; Client Group Model-&gt; find(&#39; list&#39;);  $这 - &gt;设置(紧凑(&#39; clientGroup&#39;));

并在您的ctp页面中执行此操作

echo $ this-&gt; Form-&gt; input(&#39; client_group_id&#39;);

答案 1 :(得分:1)

您是否尝试过使用驼峰式情况设置变量? $clientGroups'clientGroups'?这是正确的命名约定。

答案 2 :(得分:0)

他们的约会很疯狂!!试着这样工作,好吗? ;)

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');