Symfony 2将验证移至实体,验证与相关实体

时间:2012-10-17 09:03:38

标签: php symfony doctrine-orm validation

我有上诉实体; SiteTariff,用户实体;关税,网站实体 如何将验证移动到实体。验证复杂:与相关实体。我试图写一个回调(在实体上)=>没有结果。

    $appeal = new Appeal();
    $form = $this->createFormBuilder($appeal);

    if ($request->isMethod('POST'))
    {
        $form->bind($request);

        $appeal->setUser($user); //addRelation

        $appeal->appeals_count = $value; //set value to public field

        if ($site_tariff_id = $form->getData()->site_tariff_id)
        {
            $siteTariff = $em->find('', $site_tariff_id);
            $appeal->setSiteTariff($siteTariff); //addRelation
        }
        else
        {
            //addError, break
        }                      


        if ($appeal->getSiteTariff()->getSite()->getUser()->getId() != $user->getId())
        {
            //addError
        }

        if ($appeal->getSiteTariff()->getTariff()->getAppealsNumber() <= $appeal->appeals_count)
        {
            //addError
        }

        if ($form->isValid())
        {
            $em->persist($appeal);
        }
    }

在回调中,我在调用$this->getSiteTariff()后没有得到$ siteTariff对象,symfony写道“非对象

    public function isSiteTariffValid(ExecutionContext $context)
    {
        var_dump($this->getSiteTariff()->getId());    
    }

1 个答案:

答案 0 :(得分:0)

您的验证码应该在您的实体内,而不是在您的控制器内。请参阅文档:http://symfony.com/doc/current/reference/constraints/Callback.html