【问题标题】:How to check if Doctrine's array collection is populated or not?如何检查 Doctrine 的数组集合是否已填充?
【发布时间】:2016-11-19 18:38:45
【问题描述】:

我的 PHP 应用程序中有一个已加载的 Doctrine 实体。

如何在不触发数据库请求的情况下检查 Doctrine 的数组集合是否已经填充了数据?

我的实体序列化逻辑中需要这个。


我不确定使用isEmpty() 的建议解决方案是一个好的解决方案,因为我不知道它是否会触发数据库调用。是吗?

【问题讨论】:

  • 集合在两种情况下可能为空,一种是数据库中没有相关实体,另一种是尚未加载相关实体。我对第二种情况感兴趣。而且我不确定提供的答案,即isEmpty() 是否真的触发了数据库调用。

标签: php doctrine-orm


【解决方案1】:
public function isCollectionPopulated()
{
    if ($this->collection instanceof PersistentCollection) {
        return $this->collection->isInitialized();
    }

    // else collection instance of ArrayCollection and already populated.
    return true;
}

【讨论】:

    猜你喜欢
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2018-04-17
    • 2017-10-23
    • 2020-04-04
    • 2016-11-03
    相关资源
    最近更新 更多