1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I did exactly like that (before you wrote it:) ). Maybe you know the way to get rid of synchronization entity which is in product entity after the form is submited? For now I'm creating new entity after submiting the form and manually setting every single field passed through the form. I would rather remove synchronization from product and then save product alone but I dont know how.
I'm running into this same issue. You could just retain the same Product entity and just set the syncronization value to null (save it's value first) before saving product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.