1

我们有两个模型,它们通过具有和属于许多(HABTM)关系相关:工作,测试。我们能够成功添加/编辑关系(我们知道是因为它们出现在连接表中),但我们无法让现有值出现在视图中。选择/复选框(我们都试过了)总是空的。

以下是模型关系:

//Job.php
public $hasAndBelongsToMany = array (
    'Test' => array (
        'classname' => 'Test', 
        'foreignKey'=>'job_id',
        'joinTable' => 'jobs_tests',
        'associatedForeignKey' => 'test_id'
    )
);

//Test.php
    public $hasAndBelongsToMany = array(
        'Job' => array(
            'className'=> 'Job',
            'joinTable'=>'jobs_tests',
            'foreignKey' => 'test_id',
            'associatedForeignKey'=> 'job_id'
            )

    );

这是/view/Jobs/edit.ctp

            echo $this->Form->select('Test', $test_options, array('class'=>'form-control', 'multiple'=>'checkbox'));
//This is always empty (nothing selected/checked). 

我们做错了什么?

更新:

这是 JobsController 操作:

public function admin_edit( $id=NULL ) {
    $this->layout = 'admin';
    if (!$id)
        $this->redirect( array('controller'=>'jobs', 'action'=>'index'));

    $this->loadModel('Company');
    $companies = $this->Company->find('all');
    $company_options = array();
    foreach ($companies as $company) {
        $company_options[ $company['Company']['id'] ] = $company['Company']['name'];
    }
    $this->set('company_options', $company_options);

    $this->loadModel('Test');
    $tests = $this->Test->find('all');
    $tests_options = array();
    foreach ($tests as $test) {
        $test_options[ $test['Test']['id'] ] = $test['Test']['name'];
    }
    $this->set('test_options', $test_options);

    $category_options = $this->Job->validCategories;

    $this->set('category_options', $category_options);

    if ($this->request->isPut() ) {
        $data = $this->request->data;
        //debug($data);exit;
        $save = $this->Job->save( $data );
        if ($save) {
            $this->Session->setFlash('Job edited');
            //$job = $this->Job->findById( $id );
        } else {
            $this->Session->setFlash('Error editting job');
        }


    } 

    $job = $this->Job->findById($id);
    $this->request->data = $job;
    $this->set('job', $job);
}

这是 admin_edit.ctp 视图中的表单:

<?php echo $this->Form->create('Job'); ?>
    <fieldset>
        <?php
            echo $this->Form->input('id', array('type'=>'hidden'));
            echo $this->Form->input('name', array('class'=>'form-control')); 
            echo $this->Form->input('email', array('class'=>'form-control'));
            echo $this->Form->input('location', array('class'=>'form-control'));

            echo '<label>Type</label>';
            echo $this->Form->select('type', array('FT'=>'Full Time', 'PT'=>'Part Time', 'IN'=>'Internship'), array('empty'=>false, 'class'=>'form-control'));

            echo '<label>Company</label>';
            echo $this->Form->select('company_id', $company_options, array('class'=>'form-control')); 
            echo $this->Form->input('short_description', array('label' => 'Short Description', 'class'=>'form-control'));
            echo $this->Form->input('full_description', array('type'=>'textarea', 'label' => 'Full Description', 'class'=>'form-control'));
            echo $this->Form->input('is_private', array('label'=>'Is Private?', 'class'=>'form-control') );

            echo '<label>Category</label>';
            echo $this->Form->select('category', $category_options, array('class'=>'form-control'));
            echo '<label>Tests</label>';            
            //echo $this->Form->select('Test.id', $test_options, array('class'=>'form-control', 'multiple'=>true));

            $selected = array();
            foreach ($job['Test'] as $test) {
                $selected[]=$test['id'];
                //$selected[ $test['id'] ] = $test['name'];
            }
            debug($selected);
            echo $this->Form->input('Test', array('type'=>'select', 'options'=>$test_options, 'class'=>'form-control', 'multiple'=>'checkbox', 'selected'=>$selected));
        ?>
    </fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>
4

2 回答 2

2

呸!这是一个难题,但解决方案很简单...... $options['selected'] 中的值是字符串(数字),这让 CakePHP 感到困惑。我们使用所有原始设置将它们转换为整数intval(),现在它可以完美运行......

这是视图中内容的修订版(注意intval()):

        $selected = array();
        foreach ($job['Test'] as $test) {
            $selected[]= intval($test['id']);
        }
         echo $this->Form->input('Test', array('type'=>'select', 'options'=>$test_options, 'class'=>'form-control', 'multiple'=>'checkbox', 'selected'=>$selected));

此外,作为旁注,这证明上述评论中几乎所有受到质疑的东西都完全正常:

  1. $options['selected']不需要是键=>值对
  2. 我们使用的模式不会覆盖 request->data 并将数据传递给表单助手。
  3. 传递给视图 ($some_variable_name) 的非camelCased 变量名称仍然被表单助手正确拾取。

希望这对其他人有用。

于 2013-11-08T21:55:58.570 回答
0

如果你通过使用 Model::find('list') 设置默认值会怎样。

//controller
$this->set('test_options', $this->YourModel->find('list'));


//view
 echo $this->Form->select('Test', $test_options, array('class'=>'form-control', 'multiple'=>'checkbox'));
于 2013-11-07T18:25:41.853 回答