【发布时间】:2013-11-07 17:46:56
【问题描述】:
我们有两个模型,它们通过具有和属于多 (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')); ?>
【问题讨论】:
-
附带说明,它是
associationForeignKey,而不是associatedForeignKey,但在遵循约定时不必定义这些值,因为CakePHP 会自动选择正确的名称。 -
除了你让你的生活变得比必要的更复杂的事实之外,如果你总是覆盖它而无法保存表单,那总是会丢失你的输入(这可能是你的问题?提交表单后丢失选择?),我不明白为什么它不应该工作,假设
$test_data和$selected确实包含预期的数据。话虽如此,看看它们到底包含什么会很有帮助。 -
@ndm $test_data 的示例:
array( (int) 0 => '5', (int) 1 => '7' )。提交后数据不会丢失。默认/当前选择看起来都很好。只有 HABTM 关联存在问题。 -
抱歉,我提到了
$test_options,而不是$test_data。此外,您似乎正在显示$selected的内容,因为根据您的控制器代码,$test_options应该是id => name格式? -
@ndm 我也很困惑。我刚刚发送的是 $test_options,但我们也尝试了 key=>value 版本:
array( (int) 5 => 'WSAT Practice', (int) 7 => 'WSAT-PE' ),但也没有用。至于数据被重置的问题......我认为这实际上并没有发生,因为我在视图中调试它并且所有数据都在那里(包括关联)。我错过了什么吗?
标签: php cakephp cakephp-2.0 has-and-belongs-to-many