Cakephp 1.3将ID设置/传递给视图

时间:2013-06-03 21:55:27

标签: php cakephp

我有一个名为Policy的表和一个名为Declination的表。政策有很多赤字。赤字属于政策。现在说,我试图在Declination表中的一个名为Policy_id的字段中保存策略的ID。不幸的是,我没有运气。我相信我的观点有问题。让我解释一下我在做什么......在我的控制器中,我正在读取1条记录,然后将该数组中的ID设置为policy_id。一旦我这样做,我通过set()将该变量发送到视图。在那里我在隐藏字段中使用该变量。当我运行我的脚本时,数据会保存,但不会填充id。我已经被困在这2天了,无法绕过它!我可能会添加一件事......我正在通过URL传递该策略中的ID。所以url看起来像localhost / site / add / xxxx-xxxx-xxxx。我不确定我是否应该从那里抓住它。如果我能够解释或指出我在哪里可以学习。如果我不应该告诉我为什么不。谢谢!

模型

 <?php

class Declination扩展AppModel {     public $ name ='Declination';

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(); ?>

2 个答案:

答案 0 :(得分:0)

我猜UiForm是某种自定义助手。如果它与Form帮助器类似,那么它在create语句中看起来像是一个错误。它应该是:

<?php echo $this->UiForm->create('Declination'); ?>
<?php echo $this->UiForm->input("Declination.{$i}.policy_id", array('type' => 'hidden', 'value' => '$policy_id')); ?>

答案 1 :(得分:0)

我明白了。我抓住了我视图中的$ id变量并将其设置为我的值。所以取而代之的是我的价值$ policy_id ....它是$ 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'); ?>
        <?php echo $this->UiForm->input("Declination.{$i}.policy_id", array('type' => 'hidden', 'value' => $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(); ?>

控制器

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