对于具有相同名称字段的两个嵌入实体,Symfony2 Validator失败

时间:2012-01-30 03:19:28

标签: php symfony validation entities

我有两个实体,股票和国家与多对一关系相关。 两者都有一个名为“首字母缩略词”的字段。

添加股票的表格显示股票首字母缩略词字段和国家列表。

当新的库存保存在控制器中时,它失败并显示消息“此值不应为空白”。它似乎是由国家首字母缩略词字段中的验证器(NotBlank)引起的。

目前有两种解决方案: 1.将股票缩写字段重命名为stock_acronym; 2.或删除国家首字母缩略词字段的验证器NotBlank。

在这两种情况下它解决了问题,但我想找到一个更清洁的解决方案......

以下是一些代码:

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

    /**
     * @var string $acronym
     *
     * @ORM\Column(name="acronym", type="string", length=10)
     * @Assert\MinLength(
     *     limit=2,
     *     message="Acronym must have at least {{ limit}} characters."
     * )
     */
    private $acronym;

    /**
     * @ORM\ManyToOne(targetEntity="Admin\GeoBundle\Entity\Country", inversedBy="country_stocks")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
     */
    protected $country;

.....

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

    /**
     * @var string $country_name
     * @Assert\NotBlank()
     * @ORM\Column(name="country_name", type="string", length=70)

     */
    private $country_name;

    /**
     * @var string $country_name
     * @Assert\NotBlank()
     * @ORM\Column(name="acronym", type="string", length=2)
     */
    private $acronym;

/**
 * @ORM\OneToMany(targetEntity="Admin\GeoBundle\Entity\Stock", mappedBy="country")
 * @var ArrayCollection $country_stocks
 */
protected $country_stocks;

.....

StockType:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('acronym'); // this is the Stock acronym
    $builder->add('country', 'entity', array(
        'class' => 'Admin\GeoBundle\Entity\Country',
        'property' => 'country_name',
        'query_builder' => function (\Admin\GeoBundle\Entity\CountryRepository $repository)
                            {return $repository->createQueryBuilder('s');}
                ));
}

.....

股票控制器:

/**
 * @Route("/admin/stock/add/{id}", name="_admin_stock_add", requirements = {"id" = "\d+"}, defaults={"id" = null})
 * @Template()
 */
public function addAction($id)
{
  $request = $this->get('request');
  $em = $this->get('doctrine')->getEntityManager();

  if($id){
    $stock = $em->getRepository('AdminGeoBundle:Stock')->find($id);
    if (null === $stock){
        throw new NotFoundHttpException('The stock to be edited could not be found');
    }
  }else{
    $stock = new \Admin\GeoBundle\Entity\Stock();
  }

  $form = $this->get('form.factory')->create(new \Admin\GeoBundle\Form\Type\StockType(), $stock);

  if ('POST' == $request->getMethod()){
      $form->bindRequest($request);
        if ($form->isValid()){
            if(!$id){
              $stock->SetCreated(new \Datetime());
              $em->persist($stock);
            }
            $em->flush();
        }else{
          var_dump($form->getErrors());
        }
  }
  return $this->redirect($this->generateUrl('_admin_stock'));  
}

0 个答案:

没有答案