实体继承后,约束验证会丢失

时间:2016-11-15 08:26:49

标签: inheritance constraints symfony

我正在使用一个实体,而这个实体继承自另一个实体。除了一件事,一切都很好。似乎对基本实体字段的服务器端验证是"丢失"。

继承类:

<canvas id="js-particles" class="particles" width="960" height="960">
  <p>text appears if browser doesn 't support canvas</p>
</canvas>

然后:

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

/**
 * TicketBase
 * @ORM\MappedSuperclass
 */
class TicketBase
{
    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     * @Assert\NotBlank()
     */
    private $title;

    /* other fields */

之后,我可以在我的Ticket实体中使用我的任何TicketBase字段没问题,但是当我在Ticket上创建表单时,我在Ticket :: title上没有服务器端验证,所以如果我得到完整性约束违反,标题是空的。

我错过了让我的验证工作的东西吗?

由于

编辑:

控制器操作:

use AppBundle\Repository\TicketRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * Ticket
 *
 * @ORM\Table(name="cheval_ticket")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\TicketRepository")
 */
class Ticket extends TicketBase
{
   /* only specific fields here */

我的联系人实体与Ticket有这种关系:

/**
 * @Route("/tickets/{uniqid}/{contactId}", name="contact_tickets")
 */
public function ticketsAction(Request $request, $contactId = null, $uniqid = null)
{
    $em = $this->getDoctrine()->getManager();
    $contact = $em->getRepository("AppBundle:Contact")
            ->findOneBy(array(
        'id' => $contactId,
        'uniqid' => $uniqid
    ));

    if ($contact === null) {
        return $this->go('contact_index');
    }

    $form = $this
            ->createForm(ContactTicketsType::class, $contact, ['method' => 'POST'])
            ->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $this->setTicketsPrice($contact);
        $this->setTicketsNbJours($contact);
        $this->save($contact);
        $this->setTicketsCodeId($contact);
        return $this->go('payement_index', [
                    'contactId' => $contact->getId(),
                    'uniqid' => $contact->getUniqid(),
                        ]
        );
    }
}

我的ContactTicketsType:

/**
 * @ORM\OneToMany(targetEntity="Ticket", mappedBy="contact", cascade={"remove", "persist"})
 */
private $tickets;

2 个答案:

答案 0 :(得分:1)

它可能来自您声明您的$ title属性为私有而非受保护。

让我们采取以下例子:

class Foo
{
    private $property;

    public function getProperty()
    {
        if (null === $this->property)
        {
            return 'foo';
        }
        return $this->property;
    }
}

class Bar extends Foo
{
    public function __construct()
    {
        $this->property = 'bar';
    }
}

$child = new Bar();

echo $child->getProperty();

此代码将返回&#39; foo&#39;因为$ property不是从类Foo继承的,所以构造方法实际上什么都不做。但如果我们改变

private $property;

protected $property;

然后

echo $child->getProperty();

将显示&#39; bar&#39;

修改

您的问题实际上来自嵌入式Ticket FormType,而不是来自继承。根据{{​​3}},您必须在Contact FormType上启用cascade_validation参数

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Contact',
        'cascade_validation' => true
    ));
}

否则只会验证您的联系人实体。

答案 1 :(得分:0)

我需要在我的集合上手动添加验证,因为在symfony 3中删除了cascade_validation,就像那样:

$builder
                ->add('tickets', CollectionType::class, array(
                    'entry_type' => TicketType::class,
                    'allow_add' => true,
                    'allow_delete' => true,
                    'label' => false,
                    'by_reference' => false
                ))