我有一個名爲Policy的表和一個名爲Declination的表。政策有許多偏差。赤字屬於政策。現在說這個,我試圖在Declination表中的一個名爲Policy_id的字段中保存一個策略的ID。不幸的是,我沒有運氣。我相信我對我的觀點有困難。讓我解釋我在做什麼..在我的控制器中,我正在讀取1條記錄,然後將該數組中的ID設置爲policy_id。一旦我這樣做,我通過set()將該變量發送到視圖。一旦出現,我在隱藏字段中使用該變量。當我運行我的腳本時,數據會保存,但是ID不會填充。我已經堅持了2天以上,無法將其包圍!我可能會添加的一件事是......我通過URL從該策略中傳遞ID。所以這個url看起來像localhost/site/add/xxxx-xxxx-xxxx。我不確定我是否應該從那裏抓住它。如果我能解釋或指出我在哪裏可以學習的地方。如果我不該請告訴我爲什麼不。謝謝!Cakephp 1.3設置/通過ID到視圖
型號
<?php
類赤緯擴展AppModel { 公共$名稱= '衰落';
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(); ?>
我試過,但我沒有運氣。據說,當我運行我的腳本$ policy_id在我的數據庫中而不是所需的ID。我知道這很小,但我不能把它放在手上。 – SkillSet