dongzhuo3376 2013-11-07 17:46
浏览 33
已采纳

CakePHP:FormHelper没有显示关联模型的默认值

We have two models which are related by a has and belongs to many (HABTM) relationship: Jobs, Tests. We are able to add/edit the relationships successfully (we know because they show up in the join table), but we can't get the existing values to appear in the view. The select/checkboxes (we've tried both) are always empty.

Here are the model relationships:

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

    );

Here is the /view/Jobs/edit.ctp

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

What are we doing wrong?

Update:

Here is the JobsController action:

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

Here is the form in the admin_edit.ctp view:

<?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')); ?>
  • 写回答

2条回答 默认 最新

  • douliu8327 2013-11-08 21:55
    关注

    PHEW! This was a stumper but the solution turned out to be simple... The values in $options['selected'] were strings (of numbers), which was confusing CakePHP. We converted them to ints using intval() and it works perfectly now, using all the original settings...

    Here's the revised version of what is in the view (notice the 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));
    

    Also, as a sidenote, this is evidence that pretty much everything that was challenged in the comments above works completely fine:

    1. $options['selected'] does not need to be key=>value pairs.
    2. The pattern we're using does not overwrite the request->data and passes the data to the form helper just fine.
    3. non-camelCased variable names passed to the view ($some_variable_name) are still picked up correctly by the form helper.

    Hopefully this comes in handy to someone else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标