Symfony 2,Doctrine 2 - 如何验证删除子父关系

时间:2015-01-15 19:43:22

标签: validation symfony doctrine-orm doctrine

我有Section(Parent),Language(子)和Currency(子)实体。它们的属性通常通过Validator验证,但我有问题如何检查用户是否要删除与Section实体连接的Currency或Language实体。 Symfony抛出异常:

An exception occurred while executing 'DELETE FROM Language WHERE id = ?' 
with params [5]: SQLSTATE[23000]: Integrity constraint violation: 1451 
Cannot delete or update a parent row: a foreign key constraint fails 
(`mywork`.`section`, CONSTRAINT `FK_E2CE437382F1BAF4` FOREIGN KEY 
(`language_id`) REFERENCES `language` (`id`))

我希望捕获此异常,然后发送正常消息。我知道我可以通过try catch块捕获此错误,但在我看来,这在Symfony中不是正确的解决方案。而且我不知道如何从Doctrine \ DBAL \ DBALException获取错误代码。

我想找到类似于Validator的解决方案。如果属性值不正确,则将消息发送给用户(客户端)。例如:

My\WebBundle\Entity\Highlight:
properties:
    abbreviation:
        - NotBlank:
            message: "The abbreviation must be not empty."
        - Length:
            max: 8
            maxMessage: "Error value <strong>{{ value }}</strong> - The abbreviation can contain max {{ limit }} characters."

我在如何捕获唯一记录和为用户写消息方面遇到同样的问题。

1 个答案:

答案 0 :(得分:2)

使用Class Constraint Validator 这种方式在验证函数内:

// ...
if ($language->getSection() !== NULL) {
    // If you're using the new 2.5 validation API (you probably are!)
    $this->context->buildViolation($constraint->message)
        ->atPath('section')
        ->addViolation();

    // If you're using the old 2.4 validation API
    /*
    $this->context->addViolationAt(
        'section',
        $constraint->message,
        array(),
        null
    );
    */
}

您可以检查唯一约束,但我认为更好的想法是使用bild-in validator

编辑: 如果我说得对,你就像下面的父母一样。尝试使用preRemove LifeCycleCallbacks

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class Parent
{
    /**
    * @var ArrayCollection
    *
    * @ORM\OneToMany(targetEntity="Child", mappedBy="parent")
    */
    private $children;

    /**
    * @ORM\PreRemove
    */
    public function deleteAllChildren()
    {
         //prevent 
         foreach ($this->getChildren() as $child) {
            $this->children->removeElement($child);
            $child->setParent(NULL);
        }
    }
}