坚持一对一关系中的实体

时间:2016-05-24 15:41:53

标签: php mysql symfony orm doctrine-orm

我有一张表QuantityProduct。我试图在产品表格中保留表格数量的id

如果产品销售时可以购买一次产品,我希望sold_id由表格数量的ID设置。

我试过这样做:

foreach ($entities as $entity) {
            $entities = new Post();
            $entities->setSold($entity);
            $em->persist($entities);
            $em->flush();
            }

不幸的是sold_id仍为null。

产品

/**
 * @ORM\OneToOne(targetEntity="Quantity", cascade={"remove"})
 * @ORM\JoinColumn(name="sold_id", referencedColumnName="id")
 */
protected $sold;

数量

    /**
     * @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;

enter image description here

添加

/**
     * @Route("/complete", name = "complete")
     * @Template()
     */
    public function completeAction(Request $request)
    {   $session = $this->getRequest()->getSession();
        $token = $this->get('payum.security.http_request_verifier')->verify($request);
        $gateway = $this->get('payum')->getGateway($token->getGatewayName());
        $gateway->execute($status = new GetHumanStatus($token));

        $commande = $status->getFirstModel();
        $em = $this->getDoctrine()->getManager();
        $entities = $em->getRepository('FLYBookingsBundle:Post')->findArray(array_keys($session->get('cart')));
        $cart = $session->get('cart');
        $model = $status->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->isCaptured()) {
            $this->getRequest()->getSession()->getFlashBag()->set(
                'paid',
                'Your payment has been successfully processed.Thank you! NOTE: You will receive an email shortly'
            );
            $commande->setStatuss("PAID");
            //$this->get('flyMailer')->sendSuccessMessageBuyer($commande);
            //$this->get('flyMailer')->sendSuccessMessageSeller($commande);
            $commande->setAmountpaid($model['PAYMENTREQUEST_0_AMT']);
            $commande->setIsActive(true);

            foreach ($entities as $entity) {
                $entities = new Quantity();
                $entities->setDesk2($entity);
                $entities->setDesk1($commande);
                $entities->setQuantity($cart[$entity->getId()]);
                $em->persist($entities);

            }

            $em->flush();

            }

        if ($status->isPending()) {
            $this->getRequest()->getSession()->getFlashBag()->set(
                'pending',
                'Your payment is still pending.'
            );
            $commande->setStatuss("PAYMENT PENDING");
            $commande->setAmountpaid($model['PAYMENTREQUEST_0_AMT']);
            $commande->setIsActive(true);


        }


        if ($status->isFailed() || $status->isCanceled()) {
            $this->getRequest()->getSession()->getFlashBag()->set(
                'canceled',
                'your payment has been canceled'
            );
            $commande->setStatuss("CANCELED");

            $commande->setAmountpaid($model['PAYMENTREQUEST_0_AMT']);
            $commande->setIsActive(true);

        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($commande);
        $em->flush();


        //$session = $this->getRequest()->getSession();
        //$session->remove('address');
        //$session->remove('cart');
        //$session->remove('commande');

        return new RedirectResponse($this->generateUrl('bills', array('commande' => $commande)));


        }

实体产品

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->id;
    }
    /**
     * Set airport
     *
     * @param array $airport
     * @return Post
     */
    public function setAirport($airport)
    {
        $this->airport = $airport;

        return $this;
    }

    /**
     * Get airport
     *
     * @return string
     */
    public function getAirport()
    {
        return $this->airport;
    }

    /**
     * Set airport1
     *
     * @param array $airport1
     * @return Post
     */
    public function setAirport1($airport1)
    {
        $this->airport1 = $airport1;

        return $this;
    }

    /**
     * Get airport1
     *
     * @return string
     */
    public function getAirport1()
    {
        return $this->airport1;
    }


    /**
     * Set departuredate
     *
     * @param \DateTime $departuredate
     * @return Post
     */
    public function setDeparturedate($departuredate)
    {
        $this->departuredate = $departuredate;

        return $this;
    }

    /**
     * Get departuredate
     *
     * @return \DateTime
     */
    public function getDeparturedate()
    {
        return $this->departuredate;
    }

    /**
     * Set arrivaldate
     *
     * @param \DateTime $arrivaldate
     * @return Post
     */
    public function setArrivaldate($arrivaldate)
    {
        $this->arrivaldate = $arrivaldate;

        return $this;
    }

    /**
     * Get arrivaldate
     *
     * @return \DateTime
     */
    public function getArrivaldate()
    {
        return $this->arrivaldate;
    }

    /**
     * Set price
     *
     * @param float $price
     * @return Post
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return float
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }

    /*
     * @param User $user
     */
    public function setUser(User $user)
    {
        $this->user = $user;

        return $this;
    }


    /**
     * Set tva
     *
     * @param \FLY\BookingsBundle\Entity\Tva $tva
     * @return Post
     */
    public function setTva(\FLY\BookingsBundle\Entity\Tva $tva)
    {
        $this->tva = $tva;

        return $this;
    }

    /**
     * Get tva
     *
     * @return \FLY\BookingsBundle\Entity\Tva
     */
    public function getTva()
    {
        return $this->tva;
    }



    /**
     * Set telephone
     *
     * @param string $telephone
     * @return Post
     */
    public function setTelephone($telephone)
    {
        $this->telephone = $telephone;

        return $this;
    }

    /**
     * Get telephone
     *
     * @return string
     */
    public function getTelephone()
    {
        return $this->telephone;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Post
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }


    /**
     * Set sold
     *
     * @param integer $sold
     * @return Post
     */
    public function setSold($sold)
    {
        $this->sold = $sold;

        return $this;
    }

    /**
     * Get sold
     *
     * @return integer
     */
    public function getSold()
    {
        return $this->sold;
    }

0 个答案:

没有答案