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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...