【问题标题】:Cakephp 1.3 Set/Pass ID to the viewCakephp 1.3 将 ID 设置/传递给视图
【发布时间】:2013-06-03 21:55:27
【问题描述】:

我有一个名为 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(); ?>

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    我猜 UiForm 是某种自定义助手。如果它类似于 Form helper,那么它看起来像是您的 create 语句中的错误。应该是:

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

    【讨论】:

    • 我试过了,但没有运气。话虽如此,当我运行我的脚本时,$policy_id 会在我的数据库中而不是所需的 id 中删除。我知道这是一件小事,但我不能指手画脚。
    【解决方案2】:

    我想通了。我抓住了我视图中的 $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'));
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-20
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2013-10-29
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多