symfony 1.4:创建“复制”动作

时间:2011-04-07 13:34:50

标签: php symfony1 copy action models

我需要为模型列表创建“复制”操作。它应该从一些模型中获取所有值,将它们填充到表单中,您可以只编辑几个字段,在按下“保存”之后它将创建新模型。目前我考虑将Edit和New操作合并为:

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
  <..>
   }

EDIT部分显示了当我使用编辑按钮时symphony运行的命令 与编辑相同的新功能只会创建新模型。

我对此感到满意:

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

我失败了。这为表单提供了模型ID,因为id是预定义的 - 它编辑,而不是创建模型。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

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

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

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相同,例如,您需要告诉表单在哪里发送数据:

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

创建并配置表单,覆盖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();

  }
}

干杯!