gpt4 book ai didi

php - 检查关联是否存在而不捕获 Doctrine2 中的异常?

转载 作者:可可西里 更新时间:2023-11-01 01:06:07 26 4
gpt4 key购买 nike

是否可以在 Doctrine2 中发出查询之前检查关联是否存在?示例:

/**
* @ORM\Entity
*/
class Product
{
/**
* @ORM\OneToMany(targetEntity="Feature", inversedBy="product")
*/
public $features;
}

我想检查(实际上不发出查询本身)关联 product.features 是否存在。

编辑:出于好奇,我正在编写一个服务(实际上是一个助手)来根据 GET 参数进行一些集合过滤:

public function initialize($entityName, $key)
{
// Defaults are empty values and empty collection
$this->values = array();
$this->collection = new ArrayCollection();

// If "$key" GET parameter is null or blank return this instance
if(is_null($value = $this->request->get($key))
|| strlen(trim($value)) == 0) return $this;

// Split the parameter value based on separator (typically a comma)
$re = '/\s*' . $this->separator . '\s*/';

// Return this instance if no values are found
if(!($set = preg_split($re, $value, 0, PREG_SPLIT_NO_EMPTY))) return $this;

// Guess the repository fully qualified name and entity name
$guesser = $this->getManagementGuesser();
$repoName = $guesser->guessRepositoryName();
$entityName = $guesser->guessEntityName();

// Get the repository for the entity and create the builder
$qb = $this->getRepository($repoName)->createQueryBuilder('e');

// Check if a relation named $key exists and throw a LogicException
$exists = $this->getEntitiesUtility()->checkRelation($entityName, $key);
if(!$exists) throw new \LogicException("Relation named '$key' not found.");

// Other stuff
}

相关部分是:

$this->getEntitiesUtility()->checkRelation($entityName, $relationName);

最佳答案

// $em being your EntityManager..

if ($em->getClassMetadata($className)->getAssociationMapping($fieldName))
{
....
}

关于php - 检查关联是否存在而不捕获 Doctrine2 中的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590505/

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