gpt4 book ai didi

php - 检查 Doctrine2 中是否存在关系的技术

转载 作者:可可西里 更新时间:2023-10-31 22:11:28 25 4
gpt4 key购买 nike

我似乎无法在 Doctrine 文档中找到任何关于如何检查一个实体是否与另一个实体存在关系的提及:

http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html

在 Doctrine 1.x 中有一个名为 exists 的方法,可以在实体上调用它来检查:

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

关于php - 检查 Doctrine2 中是否存在关系的技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6224848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com