繁体   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