I also need to create a copy action in symfony. I already looked at these question and answer: symfony 1.4: creating "Copy" action. It seems to work fine, but when I try to save a get these error message: “csrf token: CSRF attack detected”. I reckon it’s because I don’t know where to change the form attribute action.
Any help much appreciated!
Here is my code:
actions.class.php:
class eventActions extends autoEventActions {
public function executeCopy(sfWebRequest $request)
{
$this->form = new EventCopyForm($this->getRoute()->getObject());
$this->event = $this->form->getObject();
$this->setTemplate('copy');
}
public function executeUpdatecopy(sfWebRequest $request)
{
$this->form = new EventCopyForm($this->getRoute()->getObject());
$this->processForm($request, $this->form);
$this->setTemplate('copy');
}
}
EvenCopyForm.class.php:
class EventCopyForm extends EventForm {
public function doSave($conn = null)
{
$this->updateObject();
$event = $this->getObject()->copy();
$event->save();
}
}
copySuccess.php:
<?php use_helper('I18N', 'Date') ?>
<?php include_partial('event/assets') ?>
<div id="sf_admin_container">
<h1><?php echo __('Copy Event', array(), 'messages') ?></h1>
<?php include_partial('event/flashes') ?>
<div id="sf_admin_header">
<?php include_partial('event/form_header', array('event' => $event, 'form' => $form, 'configuration' => $configuration)) ?>
</div>
<div id="sf_admin_content">
<?php include_partial('event/form', array('event' => $event, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?>
</div>
<div id="sf_admin_footer">
<?php include_partial('event/form_footer', array('event' => $event, 'form' => $form, 'configuration' => $configuration)) ?>
</div>
</div>
I have done it now in a different way:
+ I get the object as an array:
then unset the id:
unset($originalValues[‘id’]);
make a copy object which I save:
$copy = new Event();
$copy->fromArray($originalValues);
$copy->save();
That works fine for me.