【问题标题】:Cakephp - Automagic select box not being createdCakephp - 未创建自动选择框
【发布时间】:2013-08-03 03:30:46
【问题描述】:

我的控制器中有以下代码

$technicians = $this->Customer->Technician->find('list');
$account_managers = $this->Customer->Account_Manager->find('list');
$this->set(compact('technicians','account_managers'));

我的用户模型中有以下代码

public $hasMany = array(
    'TicketComment' => array(
        'className' => 'TicketComment',
        'foreignKey' => 'user_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
    'CustomerTech' => array(
        'className' => 'Customer',
        'foreignKey' => 'technician'
    ),
    'CustomerManager' => array(
        'className' => 'Customer',
        'foreignKey' => 'account_manager'
    )
);

我的客户模型中有以下代码

public $belongsTo = array(
    'Technician' => array(
        'className' => 'User',
        'foreignKey' => 'technician',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Account_Manager' => array(
        'className' => 'User',
        'foreignKey' => 'account_manager',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

我认为有以下代码

<div class="control-group">
    <?php echo $this->Form->label('technician', 'Technician', array('class' => 'control-label'));?>
    <div class="controls">
        <?php echo $this->Form->input('technician', array('class' => 'span12')); ?>
    </div><!-- .controls -->
</div><!-- .control-group -->

<div class="control-group">
    <?php echo $this->Form->label('account_manager', 'Account Manager', array('class' => 'control-label'));?>
    <div class="controls">
        <?php echo $this->Form->input('account_manager', array('class' => 'span12')); ?>
    </div><!-- .controls -->
</div><!-- .control-group -->

“技术员”在我看来显示为一个适当的自动选择框,但“Account_Manager”显示为一个简单的文本输入框。据我所知,两者的设置相同。那么为什么第二个输入没有形成选择框呢?

【问题讨论】:

    标签: php cakephp cakephp-2.0


    【解决方案1】:

    首先,它的 AccountManager(没有_)作为模型的名称。

    $accountmanagers = $this->Customer->Account_Manager->find('list');
    $this->set(compact('technicians','account_managers'));
    

    你不能说 $var 并传递 $other 并期望 $var 在视图中可用:)

    所以它应该(逻辑上)阅读:

    $accountManagers = $this->Customer->AccountManager->find('list');
    $this->set(compact('technicians','accountManagers'));
    

    还要注意 camelBacked 属性大小写。

    只有当你有一个名为“account_manager_id”的字段时它仍然会显示(在引用其他字段的主键时你应该根据约定)。

    【讨论】:

    • 我发布的控制器代码有误。我现在已经纠正了。我的代码实际上有 $account_managers 作为变量名。
    • 另外,Account_Manager 不是模型,而是关联的别名,如您在上面的模型代码中所见。我的客户表有一个名为 account_manager 的字段,它是用户表的外键。这有帮助吗?
    • 对别名也无效。它们需要具有相同的正确命名方案(CamelCaseStyle)。如果您不遵循约定(名为 account_manager 的字段),则需要在此处手动告诉表单助手您正在从 $accountManagers 数组中获取选项。
    • 我修改了别名、控制器和视图。我的控制器现在有 $accountManagers = $this->Customer->AccountManager->find('list'); $this->set(compact('technicians','accountManagers'));我在视图中引用它并且它工作正常。我得到了我正在寻找的选择框!但是,当我保存新客户时,客户经理没有保存。保存前我需要在控制器中做些什么吗?
    猜你喜欢
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多