我在理解 CakePHP 进行关联的方式时遇到了很多问题。 考虑以下数据库配置:
我在 CakePHP 代码中处理关系,如下所示:
抽象组件
<?php
App::uses('AppModel', 'Model');
/**
* AbstractComponent Model
*
*/
class AbstractComponent extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'id';
public $hasOne = array(
'DetailComponent' => array(
'className' => 'DetailComponent',
'foreignKey' => 'component_id'
)
);
public $hasMany = array(
'TargetComponent' => array(
'className' => 'ListComponent',
'foreignKey' => 'target_component_id'
),
'ListComponent' => array(
'className' => 'ListComponent',
'foreignKey' => 'component_id'
)
);
}
列表组件
<?php
App::uses('AppModel', 'Model');
/**
* ListComponent Model
*
*/
class ListComponent extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'id';
public $belongsTo = array(
'AbstractComponent' => array(
'className' => 'AbstractComponent',
'foreignKey' => 'component_id'
)
);
public $hasOne = array(
'TargetComponent' => array(
'className' => 'TargetComponent',
'foreignKey' => 'list_component_id'
)
);
}
目标组件
<?php
App::uses('AppModel', 'Model');
/**
* TargetComponent Model
*
*/
class TargetComponent extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'id';
public $belongsTo = array(
'ListComponent' => array(
'className' => 'ListComponent',
'foreignKey' => 'list_component_id'
)
);
}
细节组件
<?php
App::uses('AppModel', 'Model');
/**
* DetailComponent Model
*
*/
class DetailComponent extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'title_label_text';
public $belongsTo = array(
'AbstractComponent' => array(
'className' => 'AbstractComponent',
'foreignKey' => 'component_id'
)
);
}
为什么每次进入 TargetComponents 的添加 View 时我都无法选择列表组件? 我如何访问这些对象?所有其他关系都正常工作,我可以选择与我的数据库中的记录对应的 ID。
这是我的 Controller 的添加功能:
public function add() {
if ($this->request->is('post')) {
$this->TargetComponent->create();
if ($this->TargetComponent->save($this->request->data)) {
$this->Session->setFlash(__('The target component has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The target component could not be saved. Please, try again.'));
}
}
$list_components = $this->TargetComponent->ListComponent->find('list');
$this->set(compact('list_components'));
}
我的观点:
<div class="targetComponents form">
<?php echo $this->Form->create('TargetComponent');?>
<fieldset>
<legend><?php echo __('Add Target Component'); ?></legend>
<?php
echo $this->Form->input('type');
echo $this->Form->input('list_components_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Target Components'), array('action' => 'index'));?></li>
</ul>
</div>
我被卡住了,显然做错了一些可怕的事情。
非常感谢您的帮助!
提前致谢
-B
最佳答案
我已经在本地 Cake 环境中使用数据库中的一些虚拟值对此进行了测试(1 用于 int 字段,Test
用于 varchar 字段)。事实证明,所有内容实际上都已正确关联,但问题出在 View 中。
出于某种原因,Cake 不会自动将值放入 list_components_id
下拉列表中(而按照惯例,它应该将 $list_components
数组放在那里)。强制选项使它对我有用。因此,在 View 中,替换:
echo $this->Form->input('list_components_id');
有了这个:
echo $this->Form->input('list_components_id', array('options' => $list_components));
这对我有用,并使用 ListComponents 模型中的 ID 填充下拉列表。
关于php - CakePHP 关联问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9217457/