微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 用于检查Doctrine2中是否存在关系的技术

在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();
}

原文地址:https://www.jb51.cc/php/131506.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐