symfony 1.4: creating “Copy” action

夙愿已清 提交于 2020-01-06 05:48:08

问题


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

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

i commed up with this:

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

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

How should i do it?


回答1:


Here's an example:

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

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 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'>

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();

  }
}

Cheers!



来源:https://stackoverflow.com/questions/5581968/symfony-1-4-creating-copy-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!