【问题标题】:Cakephp FormHelper Create select boxCakephp FormHelper 创建选择框
【发布时间】:2012-04-10 11:46:26
【问题描述】:

我是 cakephp 初学者。

我的员工模型,

         class Employee extends AppModel {
            var $belongsTo = array(
                'Department'=>array(
                'className'=>'Department',
                'foreignKey'=>'department_id',
                'conditions'=>null,
                'fields'=>null
               )
         );
          blah--

现在在员工 add.ctp 中,我想创建一个列出所有部门的选择框。 我正在阅读官方 cakephp 2.1 文档 (here) 它告诉我添加

  $this->set('departments', $this->Employee->Department->find('list')); 

在我的控制器中..

我不知道放在哪个控制器中?它在EmployeesController 还是DepartmentsController 中?以及控制器的哪个动作?

查看创建选择框(在 add.ctp 中)

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

【问题讨论】:

    标签: cakephp cakephp-2.0 form-helpers cakephp-2.1


    【解决方案1】:

    你几乎是正确的 - 只是一个小故障:

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

    您需要按照数据库中的字段命名。 如果它是 BelongsTo 关系,那么您的员工表中应该有一个 department_id 外键。

    PS:cake 知道,如果您传递 $departments,则该数组将需要成为该表单字段的选项。所以不需要额外的配置!

    // in your add action at the very bottom
    $departments = $this->Employee->Department->find('list');
    $this->set(compact('departments')); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多