4

我有上诉实体;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);
        }
    }

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

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

1 回答 1

0

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

于 2013-01-23T16:32:27.147 回答