簡體   English   中英

cakephp:在單個視圖中添加和編輯

[英]cakephp: add and edit in single view

我在cakephp中工作,是新手。 我創建了一個視圖以將數據添加到數據庫中。 我希望編輯記錄-當用戶單擊“編輯”時,應該會出現相同的添加視圖,其中充滿了添加的數據。 我的問題是我在表中有外鍵,當我單擊“編輯”時,文本框填充了外鍵ID,而不是存儲在該ID中的文本。 而且我在某些字段的添加視圖中使用了一些jQuery。 因此,在編輯這些字段時根本不會顯示這些字段。 以下是代碼:

public function ride_offer($id = NULL) {

         if (empty($this->data)) {

        $this->data = $this->Rideoffer->read(null, $id);
      // debug($this->data); die;
    }

}

ride_offer是添加ride_offer -我希望在編輯中打開該ride_offer並用添加的數據重新填充。

<?php echo $this->Form->text('Rideoffer.DropAt', 
                                            array('class' => 'address-text',
                                               ));  ?>

只是一個例子-Rideoffer.DropAt顯示id而不是存儲在Place表中的實際地址。 Rideoffer表將Place的引用存儲為DropAt。

我該如何解決?

如果不遵守cakephp的命名約定,您真的會使自己感到困難。 這就是大多數魔術發生的方式。 您還可以使用此變形器查詢來幫助您命名。

  1. 您的操作應為ride_offer.ctp而控制器的操作應為public function rideOffer(){ 是的,這仍然可以按照您的方式進行,但不是首選的Cake Convention。

  2. 由ID代替val填充的表單字段的命名約定應為* _id。 我真的不確定您如何在<?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?>中獲取任何類型的id<?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?> <?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?>表單字段。

如果是相關表。 名字應該是

<?php echo $this->Form->text('drop_at_id', 
                                            array('class' => 'address-text',
                                               ));  ?>

然后您的控制器將模型傳遞給視圖,從而神奇地填充了這些值。

//in your controller
$this->set('dropAts', $this->RideOffer->DropAt->find('list'));

為了回應您的評論,您的ride_offer表應包含名為drop_at_id 而不是 dropAt的記錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM