我的编辑操作有问题,我不确定我的问题是什么,但是我的字段没有填充。在我的模型中,我有一个函数,它根据一个id检索3条记录,然后在我的控制器中调用该函数,而该函数又应该在视图中填充数据。我试过调试但没有运气。我试过使用die(debug($ this-> data));但是当我运行脚本时,下一页加载而不是显示$ this->数据中的内容,这对我来说很奇怪!话虽如此,这就是我所拥有的。任何投入将不胜感激!谢谢!
模型
public function declinationsForPolicyId($id = null) {
if ($id) {
$items = $this->find('all', array(
'conditions' => array(
'Declination.policy_id' => $id
),
'limit' => 3,
'contain' => array()
));
foreach ($items as $item) {
$ret= $item['Declination'];
}
$ret = array('Declination' => $ret);
}
return $ret;
}
控制器
public function edit($id = null) {
if (!empty($this->data)) {
die(debug($this->data));
$this->Declination->create();
$this->data = $this->Declination->declinationsForPolicyId($id);
if ($this->Declination->saveAll($this->data['Declination'])) {
$this->Session->setFlash(__('Declinations saved.', true));
$this->redirect(array(
'controller' => 'policies',
'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'));
}
查看
<div class="content" id="container">
<?php echo $this->UiForm->create('Declination', array(
'url' => array(
'controller' => 'declinations',
'action' => 'add',
$id
)
)); ?>
<?php for ($i = 0; $i < 3; $i++): ?>
<section class="panel">
<h4>Declination <?php echo ($i + 1); ?></h4>
<?php echo $this->UiForm->input("Declination.{$i}.policy_id", array(
'type' => 'hidden',
'value' => $id
)); ?>
<?php echo $this->UiForm->input("Declination.{$i}.id", array(
'type' => 'hidden'
)); ?>
<?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' => ''
)); ?>
</section>
<?php endfor; ?>
<?php echo $this->UiForm->end(array('label'=>'Continue', 'class'=>'btn success large fr')); ?>
答案 0 :(得分:0)
当数据不为空时,您正在检索信息,但是当它为空时(首次加载页面时),您只需填写id, reasons, contactType
。
public function edit($id = null) {
if (!empty($this->data)) {
//everything you posted
} else {
$this->request->data = $this->Declination->declinationsForPolicyId($id);
}
}
$reasons = $this->Declination->Reason->find('list');
$contactTypes = $this->Declination->ContactType->find('list');
$this->set(compact('id', 'reasons', 'contactTypes'));
}
答案 1 :(得分:0)
我解决了我的问题。我在整个申请过程中做了一些更改。我开始通过编辑html链接传递策略ID。之后我的功能似乎有效,因为id被设置为策略ID。我还对我的编辑功能稍作修改。您可以在下面看到更改。
控制器
public function edit($id = null) {
if (empty($this->data)) {
$this->data = $this->Declination->declinationsForPolicyId($id);
} else {
// some code
$this->Session->setFlash(__('Declinations failed to save.', true));
}
}