1

我有一个与实体ProductLocale关联oneToMany的产品表单,用户可以根据需要添加许多ProductLocale

html 中呈现的表单似乎是正确的,但是当我收到 POST 数组并执行bind()服务器响应时出现此错误:

"ERROR: This value should be of type Wearplay\WearBundle\Entity\ProductLocale.

但是我发送了两个ProductLocale实体,但验证器无法识别它们。

很明显POST请求包含一个多维数组,其中包含各种实体ProductLocale,但问题是为什么

$form->bindRequest($request)

不能正常工作?

编辑#1:

产品实体

<?php

namespace Wearplay\WearBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="product")
*/

class Product
{
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
     *@ORM\Column(type="string")
     */
    private $name;

    /** 
     * @ORM\ManyToOne(targetEntity="BrandOwner", cascade="persist") 
     */
    private $owner;

    /**
     * @ORM\Column(name="creation_date", type="datetime", nullable=false)
     */
    private $creationDate;

    /**
     * @Assert\Type(type="Wearplay\WearBundle\Entity\ProductLocale")
     */
    protected $productLocales;

    /**
     * @Assert\Type(type="Wearplay\WearBundle\Entity\ProductPic")
     */
    protected $productPic;

    public function __construct()
    {
        $this->productLocales = new ArrayCollection();
    }

    /** 
     * @ORM\PrePersist
     */ 
    public function createTimestamps()
    {
        $this->creationDate = new \DateTime(date('Y-m-d H:i:s'));
    }

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

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

        return $this;
    }

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

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

    /**
     * Set owner
     *
     * @param \Wearplay\WearBundle\Entity\BrandOwner $owner
     * @return Product
     */
    public function setOwner(\Wearplay\WearBundle\Entity\BrandOwner $owner = null)
    {
        $this->owner = $owner;

        return $this;
    }

    /**
     * Get owner
     *
     * @return \Wearplay\WearBundle\Entity\BrandOwner 
     */
    public function getOwner()
    {
        return $this->owner;
    }


    /**
     * Get ProductLocales
     *
     * @return \Wearplay\WearBundle\Entity\ProductLocale 
     */
    public function getProductLocales()
    {
        return $this->productLocales;
    }

    /**
     * Set ProductLocale
     *
     * @param \Wearplay\WearBundle\Entity\ProductLocale $productLocale
     * @return Product
     */
    public function setProductLocales(ArrayCollection $productLocales = null)
    {
        $this->productLocales = $productLocales;

        //return $this->productLocale;
    }

    /**
    * Add ProductLocale
    *
    * @param ProductLocale $productLocale
    */
    /*
    public function addProductLocale(ProductLocale $productLocale)
    {
        $this->productLocales[] = $productLocale;
    }

    public function removeProductLocale(ProductLocale $productLocale)
    {
        $this->productLocale->removeElement($productLocale);
    }*/

       /**
     * Get ProductPic
     *
     * @return \Wearplay\WearBundle\Entity\ProductPic
     */
    public function getProductPic()
    {
        return $this->productPic;
    }

    /**
     * Set ProductPic
     *
     * @param \Wearplay\WearBundle\Entity\ProductPic $productPic
     * @return Product
     */
    public function setProductPic(ProductPic $productPic = null)
    {
        $this->productPic = $productPic;
    }

    /**
    * Add ProductPic
    *
    * @param ProductPic $productPic
    */
    public function addProductPic(ProductPic $productPic)
   {
       $this->productPic[] = $productPic;
   }
}

ProductLocale 实体

<?php

namespace Wearplay\WearBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="product_locale")
*/

class ProductLocale
{
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /** 
     * @ORM\ManyToOne(targetEntity="Product", cascade="persist") 
     */
    private $product;

    /**
     *@ORM\Column(name="market_nation", type="string")
     */
    private $marketNation;

    /**
     * @ORM\Column(type="string")
     */
    private $link;

    /**
     * @ORM\Column(type="decimal", precision=9, scale=2)
     */
    private $price;

    /**
     * @ORM\Column(type="string", length=3)
     */
    private $currency;

    /**
     * @ORM\Column(name="creation_date", type="datetime", nullable=false)
     */
    private $creationDate;

    /** 
     * @ORM\PrePersist
     */ 

    /*
    public function addProduct(Product $product)
    {
        if (!$this->product->contains($product)) {
            $this->product->add($product);
        }
    }
    */
    public function createTimestamps()
    {
        $this->creationDate = new \DateTime(date('Y-m-d H:i:s'));
    }


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

    /**
     * Set marketNation
     *
     * @param string $marketNation
     * @return ProductLocale
     */
    public function setMarketNation($marketNation)
    {
        $this->marketNation = $marketNation;

        return $this;
    }

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

    /**
     * Set link
     *
     * @param string $link
     * @return ProductLocale
     */
    public function setLink($link)
    {
        $this->link = $link;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set currency
     *
     * @param string $currency
     * @return ProductLocale
     */
    public function setCurrency($currency)
    {
        $this->currency = $currency;

        return $this;
    }

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

    /**
     * Set creationDate
     *
     * @param \DateTime $creationDate
     * @return ProductLocale
     */
    public function setCreationDate($creationDate)
    {
        $this->creationDate = $creationDate;

        return $this;
    }

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

    /**
     * Set product
     *
     * @param \Wearplay\WearBundle\Entity\Product $product
     * @return ProductLocale
     */
    public function setProduct(\Wearplay\WearBundle\Entity\Product $product = null)
    {
        $this->product = $product;

        return $this;
    }

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

}
4

1 回答 1

0

解决了

是的@ihsan 和@snyx,你说得对,我删除了这一行: @Assert\Type(type="Wearplay\WearBundle\Entity\ProductLocale一切都很完美。

对于这个琐碎的问题,我很抱歉,但我在 Symfony2 中还很新。

于 2013-05-15T10:30:22.377 回答