我有一个名为 Policy 的表和一个名为 Declination 的表。政策有很多偏角。拒绝属于政策。话虽如此,我正在尝试将策略的 ID 保存在 Declination 表中名为 Policy_id 的字段中。不幸的是,我没有运气。我相信我的观点有问题。让我解释一下我在做什么......在我的控制器中,我正在读取 1 条记录,然后将该数组中的 ID 设置为 policy_id。一旦我这样做了,我就会通过 set() 将该变量发送到视图。在那里,我在隐藏字段中使用该变量。当我在那里运行我的脚本时,数据会保存,但不会填充 id。我已经被困在这两天多了,无法绕开它!我可能会添加一件事..我正在通过 URL 传递来自该策略的 ID。所以 url 看起来像 localhost/site/add/xxxx-xxxx-xxxx。我不确定我是否应该从那里抓住它。如果我应该你能解释或指出我可以在哪里学习的地方。如果我不应该,请告诉我为什么不。谢谢!
模型
<?php
类偏角扩展 AppModel { public $name = '偏角';
public $virtualFields = array(
'name' => 'CONCAT(Declination.first_name,\' \',Declination.last_name)'
);
public $belongsTo = array(
'Policy' => array(
'className' => 'Policy',
'foreignKey' => 'policy_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Reason' => array(
'className' => 'Reason',
'foreignKey' => 'reason_id'
),
'ContactType' => array(
'className' => 'ContactType',
'foreignKey' => 'contact_type_id'
)
);
public function beforeSave() {
if (array_key_exists('dated', $this->data[$this->alias]) && !empty($this->data[$this->alias]['dated'])) {
$this->data[$this->alias]['dated'] = date('Y-m-d', strtotime($this->data[$this->alias]['dated']));
}
if(array_key_exists('reason_id', $this->data[$this->alias])) {
if (!$this->Reason->isOther($this->data[$this->alias]['reason_id'])) {
$this->data[$this->alias]['other'] = null;
}
}
return true;
}
控制器
class DeclinationsController extends AppController {
public $name = 'Declinations';
/**
* add function.
*
* @access public
* @param mixed $id (default: null)
* @return void
*/
public function add($id = null) {
if (!empty($this->data)) {
$this->loadmodel('Policy');
$policy = $this->Policy->read('id', $id);
$policy_id = $policy['Policy']['id'];
$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', '$policy_id'));
}
看法
<?php echo $this->Ui->widgetHeader(__('Policy Declinations', true)); ?>
<?php $this->Ui->widgetContent(); ?>
<?php echo $this->UiForm->create('Declination', array(
'url' => array(
'controller' => 'declinations',
'action' => 'add',
$id
)
)); ?>
<?php for ($i = 0; $i < 3; $i++): ?>
<h4>Declination <?php echo ($i + 1); ?></h4>
<?php echo $this->UiForm->create("Declination.{$i}.policy_id", array('type' => 'hidden', 'value' => '$policy_id')); ?>
<?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' => ''
)); ?>
<?php endfor; ?>
<?php echo $this->UiForm->end('Continue'); ?>
<?php echo $this->Ui->widgetContentEnd(); ?>