0

在阅读并应用所有有关下拉列表的建议答案后,我的下拉列表中仍然没有结果。我是新手,解决这个问题真的让我头疼。我有与 belongsTO 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', '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'));
    ?>
4

3 回答 3

2

您有与 belongsTO Client_Group 表相关联的客户表。您想在我的下拉列表中显示客户组的数据。

$clientGroup = $this->客户端模型->客户端组模型->find('list'); $this->set(compact('clientGroup'));

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

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

于 2015-09-24T10:00:38.037 回答
1

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

于 2013-05-19T04:33:18.063 回答
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');
于 2013-05-19T06:36:57.280 回答