Pregunta

I have a Product entity which have option named synchronization setting which are stored in another table. Product is related with SynchronizationSetting using OneToOne. I've a little problem with persisting new product. My annotations of SynchronizationSetting look like that:

/**
 * @var \Webrama\ProductBundle\Entity\Product
 *
 * @ORM\OneToOne(targetEntity="Webrama\ProductBundle\Entity\Product")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_product", referencedColumnName="id")
 * })
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $idProduct;

Here is the table structure:

CREATE TABLE IF NOT EXISTS `synchronization_setting` (
  `id_product` int(10) unsigned NOT NULL,
  `local` char(1) DEFAULT '0',
  `internet` char(1) DEFAULT '0',
  PRIMARY KEY (`id_product`),
  KEY `id_product` (`id_product`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `synchronization_setting`
  ADD CONSTRAINT `fk_ss_id_product` FOREIGN KEY (`id_product`) REFERENCES `product` (`id`) ON UPDATE CASCADE;

I'm trying to insert new product with this code:

if($request->getMethod() == 'POST')
            {
                $form->handleRequest($request);

                if($form->isValid())
                {
                    $product = $form->getData();

                    $synchronizationSetting = $product->retSynchronizationSetting();
                    $synchronizationSetting->setIdProduct($product);
                    $em->persist($synchronizationSetting);
                    $em->flush();

                    $em->persist($product);
                    $em->flush();

                    $this->get('session')->getFlashBag()->add('notice', $this->get('translator')->trans('save_ok'));

                    return $this->redirect($this->generateUrl("webrama_product_index"));
                }
            }

The operations fails becouse product does no have related synchronization setting entity at the moment of inserting. The error is:

Entity of type Webrama\ProductBundle\Entity\SynchronizationSetting has identity through a foreign entity Webrama\ProductBundle\Entity\Product, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist 'Webrama\ProductBundle\Entity\SynchronizationSetting'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.

What I have to to is:

  • Create synchronization setting entity with idProduct before I insert the product,
  • Insert new Product to database

To do that I would make an insert to synchronization setting table before persisting the product but I'm pretty sure the is a better way to do that using Product and SynchronizationSetting entities given me by form. The question is: what way?

¿Fue útil?

Solución

I'm afraid the easiest way to go around this will be to just flush product to DB so it gets it's primary key. After that relate synchronization to already flushed product and flush synchronization itself.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top