title: Persist an entity in One-To-One Relationship
body: <p>I have a table <code>Quantity</code> and <code>Product</code>. I'm trying to persist the <code>id</code> of the table quantity in the Product table.</p>
  
  <p>As a product can be purchased once if the product is sold i want <code>sold_id</code> to be set by the id of the table Quantity.</p>
  
  <p>I have tried to do this :</p>
  
  <pre><code>foreach ($entities as $entity) {
              $entities = new Post();
              $entities-&gt;setSold($entity);
              $em-&gt;persist($entities);
              $em-&gt;flush();
              }
  </code></pre>
  
  <p>unfortunately sold_id remain null. </p>
  
  <p><strong>Product</strong></p>
  
  <pre><code>/**
   * @ORM\OneToOne(targetEntity="Quantity", cascade={"remove"})
   * @ORM\JoinColumn(name="sold_id", referencedColumnName="id")
   */
  protected $sold;
  </code></pre>
  
  <p><strong>Quantity</strong></p>
  
  <pre><code>    /**
       * @ORM\ManyToOne(targetEntity="Commandes", inversedBy="quantities")
       * @ORM\JoinColumn(name="order_id", referencedColumnName="id")
       */
      protected $desk1;
  
      /**
       * @ORM\ManyToOne(targetEntity="Post")
       * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
       */
      protected $desk2;
  
      /** @ORM\Column(name="quantity", type="integer") */
      protected $quantity;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/B6hJG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B6hJG.jpg" alt="enter image description here"></a></p>
  
  <p><strong>ADD:</strong></p>
  
  <pre><code>/**
       * @Route("/complete", name = "complete")
       * @Template()
       */
      public function completeAction(Request $request)
      {   $session = $this-&gt;getRequest()-&gt;getSession();
          $token = $this-&gt;get('payum.security.http_request_verifier')-&gt;verify($request);
          $gateway = $this-&gt;get('payum')-&gt;getGateway($token-&gt;getGatewayName());
          $gateway-&gt;execute($status = new GetHumanStatus($token));
  
          $commande = $status-&gt;getFirstModel();
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $entities = $em-&gt;getRepository('FLYBookingsBundle:Post')-&gt;findArray(array_keys($session-&gt;get('cart')));
          $cart = $session-&gt;get('cart');
          $model = $status-&gt;getModel();
          // you have order and payment status
          // so you can do whatever you want for example you can just print status and payment details.
          if ($status-&gt;isCaptured()) {
              $this-&gt;getRequest()-&gt;getSession()-&gt;getFlashBag()-&gt;set(
                  'paid',
                  'Your payment has been successfully processed.Thank you! NOTE: You will receive an email shortly'
              );
              $commande-&gt;setStatuss("PAID");
              //$this-&gt;get('flyMailer')-&gt;sendSuccessMessageBuyer($commande);
              //$this-&gt;get('flyMailer')-&gt;sendSuccessMessageSeller($commande);
              $commande-&gt;setAmountpaid($model['PAYMENTREQUEST_0_AMT']);
              $commande-&gt;setIsActive(true);
  
              foreach ($entities as $entity) {
                  $entities = new Quantity();
                  $entities-&gt;setDesk2($entity);
                  $entities-&gt;setDesk1($commande);
                  $entities-&gt;setQuantity($cart[$entity-&gt;getId()]);
                  $em-&gt;persist($entities);
  
              }
  
              $em-&gt;flush();
  
              }
  
          if ($status-&gt;isPending()) {
              $this-&gt;getRequest()-&gt;getSession()-&gt;getFlashBag()-&gt;set(
                  'pending',
                  'Your payment is still pending.'
              );
              $commande-&gt;setStatuss("PAYMENT PENDING");
              $commande-&gt;setAmountpaid($model['PAYMENTREQUEST_0_AMT']);
              $commande-&gt;setIsActive(true);
  
  
          }
  
  
          if ($status-&gt;isFailed() || $status-&gt;isCanceled()) {
              $this-&gt;getRequest()-&gt;getSession()-&gt;getFlashBag()-&gt;set(
                  'canceled',
                  'your payment has been canceled'
              );
              $commande-&gt;setStatuss("CANCELED");
  
              $commande-&gt;setAmountpaid($model['PAYMENTREQUEST_0_AMT']);
              $commande-&gt;setIsActive(true);
  
          }
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($commande);
          $em-&gt;flush();
  
  
          //$session = $this-&gt;getRequest()-&gt;getSession();
          //$session-&gt;remove('address');
          //$session-&gt;remove('cart');
          //$session-&gt;remove('commande');
  
          return new RedirectResponse($this-&gt;generateUrl('bills', array('commande' =&gt; $commande)));
  
  
          }
  </code></pre>
  
  <p><strong>Entity Product</strong> </p>
  
  <pre><code>class Post
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
  
      private $id;
      /**
       * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Tva")
       * @ORM\joinColumn(onDelete="SET NULL")
       */
      private $tva;
  
      /**
       * @var string
       *
       * @ORM\Column(name="telephone", type="string", length=30)
       */
      private $telephone;
  
      /**
       * @var string
       *
       * @ORM\Column(name="description", type="text" , length=125)
       */
      private $description;
      /**
       * @var string
       *
       * @ORM\Column(name="airport", type="string")
       */
  
      protected $airport;
      /**
       * @var string
       *
       * @ORM\Column(name="airport1", type="string")
       */
  
      protected $airport1;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="departuredate", type="datetime")
       */
      private $departuredate;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="arrivaldate", type="datetime")
       */
      private $arrivaldate;
  
      /**
       * @var float
       *
       * @ORM\Column(name="price", type="float")
       */
      private $price;
  
      /**
       *
       *
       * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User")
       * @ORM\JoinColumn(onDelete="CASCADE")
       * @Security("user.getId() == post.getUser()")
       */
      private $user;
      /**
       * @ORM\OneToOne(targetEntity="Quantity", cascade={"remove"})
       * @ORM\JoinColumn(name="sold_id", referencedColumnName="id")
       */
      protected $sold;
      /**
       * Get id
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
      /**
       * Set airport
       *
       * @param array $airport
       * @return Post
       */
      public function setAirport($airport)
      {
          $this-&gt;airport = $airport;
  
          return $this;
      }
  
      /**
       * Get airport
       *
       * @return string
       */
      public function getAirport()
      {
          return $this-&gt;airport;
      }
  
      /**
       * Set airport1
       *
       * @param array $airport1
       * @return Post
       */
      public function setAirport1($airport1)
      {
          $this-&gt;airport1 = $airport1;
  
          return $this;
      }
  
      /**
       * Get airport1
       *
       * @return string
       */
      public function getAirport1()
      {
          return $this-&gt;airport1;
      }
  
  
      /**
       * Set departuredate
       *
       * @param \DateTime $departuredate
       * @return Post
       */
      public function setDeparturedate($departuredate)
      {
          $this-&gt;departuredate = $departuredate;
  
          return $this;
      }
  
      /**
       * Get departuredate
       *
       * @return \DateTime
       */
      public function getDeparturedate()
      {
          return $this-&gt;departuredate;
      }
  
      /**
       * Set arrivaldate
       *
       * @param \DateTime $arrivaldate
       * @return Post
       */
      public function setArrivaldate($arrivaldate)
      {
          $this-&gt;arrivaldate = $arrivaldate;
  
          return $this;
      }
  
      /**
       * Get arrivaldate
       *
       * @return \DateTime
       */
      public function getArrivaldate()
      {
          return $this-&gt;arrivaldate;
      }
  
      /**
       * Set price
       *
       * @param float $price
       * @return Post
       */
      public function setPrice($price)
      {
          $this-&gt;price = $price;
  
          return $this;
      }
  
      /**
       * Get price
       *
       * @return float
       */
      public function getPrice()
      {
          return $this-&gt;price;
      }
  
      /**
       * @return User
       */
      public function getUser()
      {
          return $this-&gt;user;
      }
  
      /*
       * @param User $user
       */
      public function setUser(User $user)
      {
          $this-&gt;user = $user;
  
          return $this;
      }
  
  
      /**
       * Set tva
       *
       * @param \FLY\BookingsBundle\Entity\Tva $tva
       * @return Post
       */
      public function setTva(\FLY\BookingsBundle\Entity\Tva $tva)
      {
          $this-&gt;tva = $tva;
  
          return $this;
      }
  
      /**
       * Get tva
       *
       * @return \FLY\BookingsBundle\Entity\Tva
       */
      public function getTva()
      {
          return $this-&gt;tva;
      }
  
  
  
      /**
       * Set telephone
       *
       * @param string $telephone
       * @return Post
       */
      public function setTelephone($telephone)
      {
          $this-&gt;telephone = $telephone;
  
          return $this;
      }
  
      /**
       * Get telephone
       *
       * @return string
       */
      public function getTelephone()
      {
          return $this-&gt;telephone;
      }
  
      /**
       * Set description
       *
       * @param string $description
       * @return Post
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
  
      /**
       * Set sold
       *
       * @param integer $sold
       * @return Post
       */
      public function setSold($sold)
      {
          $this-&gt;sold = $sold;
  
          return $this;
      }
  
      /**
       * Get sold
       *
       * @return integer
       */
      public function getSold()
      {
          return $this-&gt;sold;
      }
  </code></pre>