2

假设我有两个实体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;
4

1 回答 1

5

使用计数约束

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

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

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

于 2016-10-13T15:20:20.243 回答