我有产品的表单,其中 oneToMany 与实体 ProductLocale ,用户可以添加多个 ProductLocale 他想要的。
html中呈现的表单似乎是正确的,但是当我收到POST数组并执行bind()
时,服务器响应此错误:
"ERROR: This value should be of type Wearplay\WearBundle\Entity\ProductLocale.
但是我发送了两个 ProductLocale 实体,验证器无法识别它们。
很明显, POST 请求包含一个包含各种实体 ProductLocale 的多维数组,但问题是为什么
$form->bindRequest($request)
无效?
<?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;
}
}
<?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;
}
}
答案 0 :(得分:0)
是的@ihsan和@snyx,你说得对,我删除了这一行:
@Assert\Type(type="Wearplay\WearBundle\Entity\ProductLocale
,一切都很完美。
我很抱歉这个琐碎的问题,但我在Symfony2中很新。