0

我的编辑操作有问题,我不确定我的问题是什么,但我的字段没有填充。在我的模型中,我有一个基于 id 检索 3 条记录的函数,我在控制器中调用该函数,该函数又应在视图中填充数据。我试图调试但没有运气。我曾尝试使用 die(debug($this->data)); 但是我在运行脚本时会加载下一页而不是显示 $this->data 中的内容,这对我来说很奇怪!话虽如此,这就是我所拥有的。任何投入将不胜感激!谢谢!

模型

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')); ?>
4

2 回答 2

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'));

}

于 2013-06-07T16:51:12.210 回答
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));
        }


    }
于 2013-06-12T03:52:32.117 回答