1

I am working with Symfony 1.4 and propel, I have a form for products to either be created or updated and for the form action I have this code...

<form action="<?php echo url_for('products/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" ?>>

so if the product object is new then it goes to products/create but if the product already exists then it goes to products/update?id=productID.

This all works great but the issue is that I have a link on the products called "New Version" When you click this it needs to take all of the data from the current product and prefill the form with it, so the user just needs to make whatever changes they need and then it saves as a new entry in the database. The problem is when the data is sent to the form through an object isNew is false so it goes to the update page instead of create.

The method for copy:

  public function executeCopy(sfWebRequest $request)
      {
        $this->forward404Unless($PlatformProduct = PlatformProductPeer::retrieveByPk($request->getParameter('id')), sprintf('Object PlatformProduct does not exist (%s).', $request->getParameter('id')));
        $this->form = new PlatformProductForm($PlatformProduct);

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

This sends the data to the form just fine but I'm at a loss for how to make it return isNew as true so that it creates a new entry. I'm fairly new to Symfony and I've worked with Cake before, in cake it was as simple as if the ID is set it will update on save, if ID is not set it will insert on save.

j0k
  • 22,600
  • 28
  • 79
  • 90
aikepah
  • 23
  • 7

1 Answers1

1

Your link could add hidden parameter to the form. And parameter could be checked by action updating an object. If it is set, then create new object instead updating old one. Info about symfony forms is here http://www.symfony-project.org/forms/1_4/en/

smentek
  • 2,820
  • 1
  • 28
  • 32
  • I feel like an idiot now lol. I think if I just do $PlatformProduct->newVersion = 1; before setting $this->form and then in the actual form I can just do if($PlatformProduct){blahblahblah} else{blah} – aikepah Jan 24 '11 at 22:31
  • It worked, so thanks for nudging my mind in the right direction. – aikepah Jan 25 '11 at 16:43