symfony 1.4:创建“复制"行动 [英] symfony 1.4: creating "Copy" action

查看:270
本文介绍了symfony 1.4:创建“复制"行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为模型列表创建复制"操作.它应该从某个模型中获取所有值,然后将其填充到表单中,您只需编辑几个字段,然后按保存"就可以创建新模型.此刻,我考虑将编辑"和新操作"这样合并:

I need to create "Copy" action for model listing. It should take all values from some model, fill those to forms, you could edit just few fields and after pressing "save" it would create NEW model. At the moment i thought about merging Edit and New actions as so:

public function executeListCopy(sfWebRequest $request)
  {
  # EDIT
  # $this->offer = $this->getRoute()->getObject();
  # $this->form = $this->configuration->getForm($this->offer);

  # NEW
  # $this->form = $this->configuration->getForm();
  # $this->offer = $this->form->getObject();

  # COPY
  <..>
   }

编辑"部分显示当我使用编辑"按钮时Symphony会运行哪些命令.
NEW与编辑相同,只是创建新模型.

EDIT section shows what commands symphony runs when i use edit button.
NEW same as edit just creates new model.

我想到了:

$this->form = $this->configuration->getForm($this->getRoute()->getObject());
$this->job_offer = $this->form->getObject();

我失败了.这给表单提供了模型ID,并且由于id是预定义的-它是编辑而不是创建模型.

And i failed. This gives model ID to the form and since id is predefined - it edits, not creates model.

我应该怎么做?

推荐答案

下面是一个示例:

//routing
job:
  class: sfDoctrineRouteCollection
  options:
    model: Job
    module: job
    object_actions: {copy: get, updatecopy: post}

创建2个操作(基于编辑和更新)

Create 2 actions (based on edit and update)

class jobActions extends sfActions
{
  public function executeCopy(sfWebRequest $request)
  {
    $this->form = new JobCopyForm($this->getRoute()->getObject());

    $this->setTemplate('copy');
  }
  public function executeUpdatecopy(sfWebRequest $request)
  {
    $this->form = new JobCopyForm($this->getRoute()->getObject());

    $this->processForm($request, $this->form);

    $this->setTemplate('copy');
  }
}

copySuccess模板与editSuccess相同,例如,您需要告诉表单将​​数据发送到哪里:

copySuccess template is the same as editSuccess, exept that you need to tell the form where to send data:

<form action='<?php echo url_for('job_updatecopy', $form->getObject()) ?>' method='post'>

创建和配置表单,覆盖doSave

Create and configure form, override doSave

class JobCopyForm extends BaseJobForm
{
  public function configure()
  {
  }

  public function doSave($conn = null)
  {
    //update object values from form values
    $this->updateObject();
    //clone object
    $job = $this->getObject()->copy();
    //save a clone
    $job->save();

  }
}

干杯!

这篇关于symfony 1.4:创建“复制"行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆