php – 用于检查Doctrine2中是否存在关系的技术
发布时间:2020-12-13 17:36:31 所属栏目:PHP教程 来源:网络整理
导读:在Doctrine文档中似乎没有提到如何检查一个实体是否与另一个实体存在关系: http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html 在教义1.x中有一种称为存在的方法,可以在一个实体上调用以检查: http://www.doctrin
在Doctrine文档中似乎没有提到如何检查一个实体是否与另一个实体存在关系:
http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html 在教义1.x中有一种称为存在的方法,可以在一个实体上调用以检查: http://www.doctrine-project.org/documentation/manual/1_2/en/working-with-models#dealing-with-relations:clearing-related-records 在Doctrine 2.0中,这是我所倾向于做的.其他人使用什么技术? <?php class Group { private $id; protected $name; protected $users; public function __construct() { $this->colorgroups = new ArrayCollection(); } public function hasUsers() { return count($this->users) > 0; } }
嗯 – 我实际上偶然发现了正确的答案,同时查看了ArrayCollection类.你应该使用’isEmpty’方法.
从代码(评论是他们的,而不是我的) /** * Checks whether the collection is empty. * * Note: This is preferrable over count() == 0. * * @return boolean TRUE if the collection is empty,FALSE otherwise. */ public function isEmpty() { return ! $this->_elements; } 所以从我的例子 public function hasUsers() { return !$this->users->isEmpty(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |