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

Php 检查关联是否存在,而不捕获Doctrine2中的异常?,php,symfony,doctrine,doctrine-orm,Php,Symfony,Doctrine,Doctrine Orm,在Doctrine2中发出查询之前,是否可以检查关联是否存在?例如: /** * @ORM\Entity */ class Product { /** * @ORM\OneToMany(targetEntity="Feature", inversedBy="product") */ public $features; } 我想检查(实际上不发出查询本身)是否存在关联product.features 编辑:出于好奇,我正在编写一个服务(实际上是一个助手)来

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

您的意思是,如果给定的产品至少有一个关联的功能记录?你必须执行一个查询才能这样做。@PeterBailey不,我知道我可以加入它。我说的是,产品是否真的有一个名为“features”的关系。获取类元数据并遍历它,以检查实体中是否定义了这种关系。不过,我想进一步了解你为什么问这个问题,因为我怀疑你正在试图解决如此多的多态性问题或类似的问题,如果你是的话,还有更好的方法可以解决。@BorisGuéry感谢你对此感兴趣。您所说的只是检查(静态地,如果您理解我的意思)是否实际存在
$features
<代码>$features当然可以存在,而不是关系本身(可能只是一个属性)。其他问题请参见我的编辑,谢谢您的帮助。
// $em being your EntityManager..

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