简体   繁体   English

cakephp:在单个视图中添加和编辑

[英]cakephp: add and edit in single view

i am working in cakephp n am new to it. 我在cakephp中工作,是新手。 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. 我的问题是我在表中有外键,当我单击“编辑”时,文本框填充了外键ID,而不是存储在该ID中的文本。 And also i have used some jquery in add view for some fields. 而且我在某些字段的添加视图中使用了一些jQuery。 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. ride_offer是添加ride_offer -我希望在编辑中打开该ride_offer并用添加的数据重新填充。

<?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.DropAt显示id而不是存储在Place表中的实际地址。 Rideoffer table stores reference of Place as DropAt. Rideoffer表将Place的引用存储为DropAt。

How do i solve this? 我该如何解决?

You are really making things difficult for yourself by not following the cakephp naming conventions . 如果不遵守cakephp的命名约定,您真的会使自己感到困难。 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(){ . 您的操作应为ride_offer.ctp而控制器的操作应为public function rideOffer(){ Yes, this should still work the way you have it but is not the preferred convention of cake. 是的,这仍然可以按照您的方式进行,但不是首选的Cake Convention。

  2. The naming convention of your form field that is being populated by id's instead of vals should be *_id. 由ID代替val填充的表单字段的命名约定应为* _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', )); ?>中获取任何类型的id<?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?> <?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?> form field. <?php echo $this->Form->text('Rideoffer.DropAt', array('class' => 'address-text', )); ?>表单字段。

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 为了回应您的评论,您的ride_offer表应包含名为drop_at_id 而不是 dropAt的记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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