gpt4 book ai didi

php - Symfony 2 将验证移至实体,使用相关实体进行验证

转载 作者:可可西里 更新时间:2023-10-31 23:49:24 24 4
gpt4 key购买 nike

我有上诉实体; 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 写“non-object

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

最佳答案

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

关于php - Symfony 2 将验证移至实体,使用相关实体进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12930707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com