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