具有反向关系的学说坚持实体不起作用

时间:2015-06-12 11:20:05

标签: symfony doctrine-orm entity

我正在尝试保存两个链接的实体。产品实体可以具有任何或许多实体ProviderRate。当我尝试保存产品实体时,它告诉我ProviderRate相关实体尚未分配其必需字段之一。我需要保存产品而无需分配ProviderRate。

显示我的错误消息是:

Entity of type AppBundle\Entity\ProviderRate is missing an assigned ID for field 'provider'.
The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called.
If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

我的授权代码如下:

产品实体

 /**
    * Products
     *
         * @ORM\Table(name="products", uniqueConstraints={@ORM\UniqueConstraint(name="id_producto_UNIQUE", columns={"id"})}, indexes={@ORM\Index(name="fk_id_productos_id_categorias1_idx", columns={"category_id"}), @ORM\Index(name="fk_id_productos_id_producto_tipo1_idx", columns={"type"}), @ORM\Index(name="fk_id_productos_id_moneda1_idx", columns={"currency_id"})})
         * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductsRepository")
         */
        class Products {
            /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=45, nullable=false)
         * @Assert\NotBlank(message="Por favor, escriba el nombre del producto.")
         */
        private $name;

        /**
         * @var string
         *
         * @ORM\Column(name="description", type="string", length=255, nullable=false)
         */
        private $description;

        /**
         * @var string
         *
         * @ORM\Column(name="code", type="string", length=45, nullable=false)
         */
        private $code;

        /**
         * @var string
         *
         * @ORM\Column(name="description_long", type="text", nullable=false)
         */
        private $descriptionLong;

        /**
         * @var integer
         *
         * @ORM\Column(name="amount_per_unit", type="integer", nullable=false)
         */
        private $amountPerUnit;

        /**
         * @var string
         *
         * @ORM\Column(name="weight", type="decimal", precision=11, scale=3, nullable=false)
         */
        private $weight;

        /**
         * @var string
         *
         * @ORM\Column(name="web", type="string", length=100, nullable=false)
         */
        private $web;

        /**
         * @var boolean
         *
         * @ORM\Column(name="isActive", type="boolean", nullable=false)
         */
        private $isactive;

        /**
         * @var \DateTime
         *
         * @ORM\Column(name="createdtime", type="datetime", nullable=false)
         * @Assert\DateTime()
         */
        private $createdtime;

        /**
         * @var \DateTime
         *
         * @ORM\Column(name="modifiedtime", type="datetime", nullable=false)
         * @Assert\DateTime()
         */
        private $modifiedtime;

        /**
         * @var \DateTime
         *
         * @ORM\Column(name="deletedtime", type="datetime", nullable=true)
         */
        private $deletedtime;

        /**
         * @var boolean
         *
         * @ORM\Column(name="isDeleted", type="boolean", nullable=false)
         */
        private $isdeleted;

        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;

        /**
         * @var \AppBundle\Entity\Categories
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Categories")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="category_id", referencedColumnName="id")
         * })
         */
        private $category;

        /**
         * @var \AppBundle\Entity\ProductTypes
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\ProductTypes")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="type", referencedColumnName="id")
         * })
         */
        private $type;

        /**
         * @var \AppBundle\Entity\Currencies
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Currencies")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
         * })
         */
        private $currency;

        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Sets", inversedBy="product")
         * @ORM\JoinTable(name="products_sets",
         *   joinColumns={
         *     @ORM\JoinColumn(name="product_id", referencedColumnName="id")
         *   },
         *   inverseJoinColumns={
         *     @ORM\JoinColumn(name="set_id", referencedColumnName="id")
         *   }
         * )
         */
        private $set;

        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Products", mappedBy="productParentid")
         */
        private $product;

        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Documents", inversedBy="product")
         * @ORM\JoinTable(name="product_attachments",
         *   joinColumns={
         *     @ORM\JoinColumn(name="product_id", referencedColumnName="id")
         *   },
         *   inverseJoinColumns={
         *     @ORM\JoinColumn(name="document_id", referencedColumnName="id")
         *   }
         * )
         */
        private $document;

        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Images", inversedBy="product")
         * @ORM\JoinTable(name="products_images",
         *   joinColumns={
         *     @ORM\JoinColumn(name="product_id", referencedColumnName="id")
         *   },
         *   inverseJoinColumns={
         *     @ORM\JoinColumn(name="image_id", referencedColumnName="id")
         *   }
         * )
         */
        private $image;

        // CUSTOM CODE
        /** 
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\ProviderRate", mappedBy="product", cascade={"persist"})
         */
        private $providerRate;

        /**
         * Constructor
         */
        public function __construct() {
            $this->createdtime      = new \DateTime();
            $this->modifiedtime     = new \DateTime();
            $this->set              = new \Doctrine\Common\Collections\ArrayCollection();
            $this->product          = new \Doctrine\Common\Collections\ArrayCollection();
            $this->document         = new \Doctrine\Common\Collections\ArrayCollection();
            $this->image            = new \Doctrine\Common\Collections\ArrayCollection();
            $this->providerRate     = new \Doctrine\Common\Collections\ArrayCollection();
            $this->descriptionLong  = '';
            $this->amountPerUnit    = 1;
            $this->web              = '';
            $this->weight           = 0;
            $this->isdeleted        = 0;
        }


        /**
         * Set name
         *
         * @param string $name
         * @return Products
         */
        public function setName($name) {
            $this->name = $name;

            return $this;
        }

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

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

            return $this;
        }

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

        /**
         * Set code
         *
         * @param string $code
         * @return Products
         */
        public function setCode($code) {
            $this->code = $code;

            return $this;
        }

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

        /**
         * Set descriptionLong
         *
         * @param string $descriptionLong
         * @return Products
         */
        public function setDescriptionLong($descriptionLong) {
            $this->descriptionLong = $descriptionLong;

            return $this;
        }

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

        /**
         * Set amountPerUnit
         *
         * @param integer $amountPerUnit
         * @return Products
         */
        public function setAmountPerUnit($amountPerUnit) {
            $this->amountPerUnit = $amountPerUnit;

            return $this;
        }

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

        /**
         * Set weight
         *
         * @param string $weight
         * @return Products
         */
        public function setWeight($weight) {
            $this->weight = $weight;

            return $this;
        }

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

        /**
         * Set web
         *
         * @param string $web
         * @return Products
         */
        public function setWeb($web) {
            $this->web = $web;

            return $this;
        }

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

        /**
         * Set isactive
         *
         * @param boolean $isactive
         * @return Products
         */
        public function setIsactive($isactive) {
            $this->isactive = $isactive;

            return $this;
        }

        /**
         * Get isactive
         *
         * @return boolean 
         */
        public function getIsactive() {
            return $this->isactive;
        }

        /**
         * Set createdtime
         *
         * @param \DateTime $createdtime
         * @return Products
         */
        public function setCreatedtime($createdtime) {
            $this->createdtime = $createdtime;

            return $this;
        }

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

        /**
         * Set modifiedtime
         *
         * @param \DateTime $modifiedtime
         * @return Products
         */
        public function setModifiedtime($modifiedtime) {
            $this->modifiedtime = $modifiedtime;

            return $this;
        }

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

        /**
         * Set deletedtime
         *
         * @param \DateTime $deletedtime
         * @return Products
         */
        public function setDeletedtime($deletedtime) {
            $this->deletedtime = $deletedtime;

            return $this;
        }

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

        /**
         * Set isdeleted
         *
         * @param boolean $isdeleted
         * @return Products
         */
        public function setIsdeleted($isdeleted) {
            $this->isdeleted = $isdeleted;

            return $this;
        }

        /**
         * Get isdeleted
         *
         * @return boolean 
         */
        public function getIsdeleted() {
            return $this->isdeleted;
        }

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

        /**
         * Set category
         *
         * @param \AppBundle\Entity\Categories $category
         * @return Products
         */
        public function setCategory(\AppBundle\Entity\Categories $category = null) {
            $this->category = $category;

            return $this;
        }

        /**
         * Get category
         *
         * @return \AppBundle\Entity\Categories 
         */
        public function getCategory() {
            return $this->category;
        }

        /**
         * Set type
         *
         * @param \AppBundle\Entity\ProductTypes $type
         * @return Products
         */
        public function setType(\AppBundle\Entity\ProductTypes $type = null) {
            $this->type = $type;

            return $this;
        }

        /**
         * Get type
         *
         * @return \AppBundle\Entity\ProductTypes 
         */
        public function getType() {
            return $this->type;
        }

        /**
         * Set currency
         *
         * @param \AppBundle\Entity\Currencies $currency
         * @return Products
         */
        public function setCurrency(\AppBundle\Entity\Currencies $currency = null) {
            $this->currency = $currency;

            return $this;
        }

        /**
         * Get currency
         *
         * @return \AppBundle\Entity\Currencies 
         */
        public function getCurrency() {
            return $this->currency;
        }

        /**
         * Add set
         *
         * @param \AppBundle\Entity\Sets $set
         * @return Products
         */
        public function addSet(\AppBundle\Entity\Sets $set) {
            $this->set[] = $set;

            return $this;
        }

        /**
         * Remove set
         *
         * @param \AppBundle\Entity\Sets $set
         */
        public function removeSet(\AppBundle\Entity\Sets $set) {
            $this->set->removeElement($set);
        }

        /**
         * Get set
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getSet() {
            return $this->set;
        }

        /**
         * Add product
         *
         * @param \AppBundle\Entity\Products $product
         * @return Products
         */
        public function addProduct(\AppBundle\Entity\Products $product) {
            $this->product[] = $product;

            return $this;
        }

        /**
         * Remove product
         *
         * @param \AppBundle\Entity\Products $product
         */
        public function removeProduct(\AppBundle\Entity\Products $product) {
            $this->product->removeElement($product);
        }

        /**
         * Get product
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getProduct() {
            return $this->product;
        }

        /**
         * Add document
         *
         * @param \AppBundle\Entity\Documents $document
         * @return Products
         */
        public function addDocument(\AppBundle\Entity\Documents $document) {
            $this->document[] = $document;

            return $this;
        }

        /**
         * Remove document
         *
         * @param \AppBundle\Entity\Documents $document
         */
        public function removeDocument(\AppBundle\Entity\Documents $document) {
            $this->document->removeElement($document);
        }

        /**
         * Get document
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getDocument() {
            return $this->document;
        }

        /**
         * Add image
         *
         * @param \AppBundle\Entity\Images $image
         * @return Products
         */
        public function addImage(\AppBundle\Entity\Images $image) {
            $this->image[] = $image;

            return $this;
        }

        /**
         * Remove image
         *
         * @param \AppBundle\Entity\Images $image
         */
        public function removeImage(\AppBundle\Entity\Images $image) {
            $this->image->removeElement($image);
        }

        /**
         * Get image
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getImage() {
            return $this->image;
        }

        // CUSTOM CODE
        /**
         * Set providerRate
         *
         * @param \AppBundle\Entity\ProviderRate $providerRate
         * @return Products
         */
     /*   public function setProviderRate(\AppBundle\Entity\ProviderRate $providerRate = null)
        {
            $this->providerRate = $providerRate;

            return $this;
        }
    */
         /**
         * Add providerRate
         *
         * @param \AppBundle\Entity\ProviderRate $providerRate
         * @return Products
         */
        public function addProviderRate(\AppBundle\Entity\ProviderRate $providerRate) {
            $this->providerRate[] = $providerRate;

            return $this;
        }

        /**
         * Remove providerRate
         *
         * @param \AppBundle\Entity\ProviderRate $providerRate
         */
        public function removeProviderRate(\AppBundle\Entity\ProviderRate $providerRate) {
            $this->providerRate->removeElement($providerRate);
        }

        /**
         * Get providerRate
         *
         * @return \AppBundle\Entity\ProviderRate 
         */
        public function getProviderRate()
        {
            return $this->providerRate;
        }
    }

ProviderRate实体

  /**
     * ProviderRate
     *
     * @ORM\Table(name="provider_rate", indexes={@ORM\Index(name="fk_proveedor_has_producto_compra_producto_compra1_idx", columns={"product_id"}), @ORM\Index(name="fk_id_tarifa_proveedor_id_moneda1_idx", columns={"currency_id"}), @ORM\Index(name="IDX_3A645C45A53A8AA", columns={"provider_id"})})
     * @ORM\Entity(repositoryClass="AppBundle\Repository\ProviderRateRepository")
     */
    class ProviderRate
    {
        /**
         * @var string
         *
         * @ORM\Column(name="reference", type="string", length=45, nullable=false)
         */
        private $reference;

        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=45, nullable=false)
         */
        private $name;

        /**
         * @var string
         *
         * @ORM\Column(name="description", type="string", length=255, nullable=false)
         */
        private $description;

        /**
         * @var integer
         *
         * @ORM\Column(name="amount_per_unit", type="integer", nullable=true)
         */
        private $amountPerUnit;

        /**
         * @var string
         *
         * @ORM\Column(name="unit_price", type="decimal", precision=25, scale=3, nullable=false)
         */
        private $unitPrice;

        /**
         * @var string
         *
         * @ORM\Column(name="discount", type="decimal", precision=25, scale=3, nullable=false)
         */
        private $discount;

        /**
         * @var \AppBundle\Entity\Providers
         *
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="NONE")
         * @ORM\OneToOne(targetEntity="AppBundle\Entity\Providers")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="provider_id", referencedColumnName="id")
         * })
         */
        private $provider;

        /**
         * @var \AppBundle\Entity\Products
         *
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="NONE")
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Products", inversedBy="providerRate")
         * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
         * 
         */
        private $product;


        /**
         * Set provider
         *
         * @param \AppBundle\Entity\Providers $provider
         * @return ProviderRate
         */
        public function setProvider(\AppBundle\Entity\Providers $provider)
        {
            $this->provider = $provider;

            return $this;
        }

        /**
         * Get provider
         *
         * @return \AppBundle\Entity\Providers 
         */
        public function getProvider()
        {
            return $this->provider;
        }

        /**
         * Set product
         *
         * @param \AppBundle\Entity\Products $product
         * @return ProviderRate
         */
        public function setProduct(\AppBundle\Entity\Products $product)
        {
            $this->product = $product;

            return $this;
        }

        /**
         * Get product
         *
         * @return \AppBundle\Entity\Products 
         */
        public function getProduct()
        {
            return $this->product;
        }

    }

在控制器上运行的代码如下:

public function ajaxNewProductAction() {
        $request = $this->getRequest();

        $product    = new Products();
        $form       = $this->createForm(new ProductsType(), $product);
        $form->handleRequest($request);
        if ($request->getMethod() == 'POST') {
            if ($form->isSubmitted() && $form->isValid()) { // Se procesa el formulario
                $em             = $this->getDoctrine()->getManager();
                $providerId     = $request->get('provider');
                $productVals    = $request->get('Products');
                $currency       = $em->getRepository('AppBundle:Currencies')->findOneByName("Euro");

                $product->setCurrency($currency);
                $product->setType($em->getRepository('AppBundle:ProductTypes')->findOneById(1));
                $product->setIsactive(1);

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


                $response['success'] = true;
                $response['data'] = 0;
                $response['providerId'] = $providerId;
            }
            else {
                $response['success'] = false;
                $response['cause'] = 'whatever';
            }
            return new JsonResponse($response);
        }

        return $this->render(':products/ajax:newProduct.html.twig', array("form" => $form->createView(), "edit" => false));
    }

1 个答案:

答案 0 :(得分:0)

SOLVE:

我可以解决它。我的实体的代码没问题。我改变了控制器功能。 首先,我创建产品对象,将providerRate设置为null,并将它们保留。 在我创建providerRate对象并设置产品对象之后。

public function ajaxNewProductAction() {
        $request = $this->getRequest();

        $product    = new Products();
        $form       = $this->createForm(new ProductsType(), $product);
        $form->handleRequest($request);
        if ($request->getMethod() == 'POST') {
            if ($form->isSubmitted() && $form->isValid()) { // Se procesa el formulario
                $em             = $this->getDoctrine()->getManager();
                $providerId     = $request->get('provider');
                $productVal     = $request->get('Products');
                $currency       = $em->getRepository('AppBundle:Currencies')->findOneByName("Euro");

                $product->setCurrency($currency);
                $product->setType($em->getRepository('AppBundle:ProductTypes')->findOneById(1));
                $product->setProviderRate(null);
                $em->persist($product);
                $em->flush();

                $providerRate = new ProviderRate();
                $providerRate->setProvider($em->getRepository('AppBundle:Providers')->findOneById($providerId));
                $providerRate->setProduct($product);
                $providerRate->setReference($productVal["providerRate"][1]["reference"]);
                $providerRate->setName($productVal["name"]);
                $providerRate->setDescription($productVal["description"]);
                $providerRate->setAmountPerUnit(1);
                $providerRate->setUnitPrice(0);
                $providerRate->setDiscount(0);
                $providerRate->setCurrency($currency);

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

                $response['success'] = true;
            }
            else {
                $response['success'] = false;
                $response['cause'] = 'Algo ocurrió';
            }
            return new JsonResponse($response);
        }

        return $this->render(':products/ajax:newProduct.html.twig', array("form" => $form->createView(), "edit" => false));
    }