我无法理解如何在 Cakephp 1.3 中保存相关模型数据。我有一个名为 Policy 的模型和一个名为 Declinations 的模型。政策有许多拒绝。拒绝属于政策。我想要做的就是将 Policy_id 保存在 Declinations 表中名为 policy_id 的字段中。那么问题是如何从对应的模型中获取 Policy_Id 呢?我是否需要先在 Declination 模型或控制器中设置 Policy_Id,然后将其传递给视图中的隐藏字段?
这是我的偏角模型
public $belongsTo = array(
'Policy' => array(
'className' => 'Policy',
'foreignKey' => 'policy_id'
我的策略模型如下
public $hasMany = array(
'Declination' => array(
'className' => 'Declination',
'foreignKey' => 'policy_id',
'dependent' => true
)
这是我的控制器
public function add($id = null) {
if (!empty($this->data)) {
$this->Declination->create();
if ($this->Declination->saveAll($this->data['Declination'])) {
$this->Session->setFlash(__('Declinations saved.', true));
$this->redirect(array(
'controller' => 'coverages',
'action' => 'view',
$id
));
} else {
$this->Session->setFlash(__('Declinations failed to save.', true));
}
}
$reasons = $this->Declination->Reason->find('list');
$contactTypes = $this->Declination->ContactType->find('list');
$this->set(compact('id', 'reasons', 'contactTypes'));
}
这是我的看法
<?php echo $this->UiForm->input("Declination.{$i}.first_name"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.last_name"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.company"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.contact_type_id"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.phone_number"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.reason_id"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.other", array(
'label' => 'If other, please supply a reason'
)); ?>
<?php echo $this->UiForm->input("Declination.{$i}.dated", array(
'type' => 'text',
'readonly' => 'readonly',
'data-datepicker' => ''
)); ?>
任何投入将不胜感激!谢谢!