简体   繁体   中英

cakephp: add and edit in single view

i am working in cakephp n am new to it. I have created a view to add data into database. I wish to edit the record- when user clicks on edit, the same add view should appear filled with the added data. My problem is that i have foreign keys in the table and when i click on edit, the textbox gets filled with foreign key id instead of text stored in that id. And also i have used some jquery in add view for some fields. So while editing those fields aren't displayed at all. Following is the code:

public function ride_offer($id = NULL) {

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

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

}

ride_offer is the add view- i wish to open the same in edit refilled with the added data.

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

just an example- Rideoffer.DropAt shows id instead of actual address stored in Place table . Rideoffer table stores reference of Place as DropAt.

How do i solve this?

You are really making things difficult for yourself by not following the cakephp naming conventions . That's how most of the magic happens. You can also use this inflector lookup to help you with naming.

  1. Your action should be ride_offer.ctp and your controller action should be public function rideOffer(){ . Yes, this should still work the way you have it but is not the preferred convention of cake.

  2. The naming convention of your form field that is being populated by id's instead of vals should be *_id. I am really not sure how you are getting any kind of id inside of your <?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?> <?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?> form field.

if it is a related table. the name should be

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

and then your controller will magically fill in the vals by passing that model through to the view.

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

In response to your comment... your ride_offer table should contain a record called drop_at_id not dropAt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM