如何创建限制最大子项的约束

Iva*_*nko 2 symfony symfony-validator

假设我有两个实体Bus并且它们之间People存在关系OneToMany.巴士最多可容纳10人.

如何创建约束来控制这个?

例如:

* @MyAssert\ParentMaxChild(max=10)

* @ORM\ManyToOne(targetEntity="Webface\CharacterBundle\Entity\Bus", inversedBy="wac")
* @ORM\JoinColumn(name="bus_id", referencedColumnName="id", nullable=false)

private $bus;
Run Code Online (Sandbox Code Playgroud)

Vev*_*eve 5

使用Count约束.

在您的Bus类中,在Person注释中添加约束:

/**
 * ... Rest of the annotation ...
 * @Assert\Count(
 *      max = "10",
 *      maxMessage = "Bus can hold a maximum of 10 persons."
 * )
 */
protected $persons;
Run Code Online (Sandbox Code Playgroud)

请注意,您可以指定min参数和相应的消息.