我有三个实体:
WordList与WordInList具有OneToMany关系。 WordInList与Definition
具有OneToMany关系我试图查询word_id =的所有定义行?和wordlist_id =?。
在标准SQL中,我会编写以下内容:
SELECT * FROM definition WHERE word_wordlist_word_id = ? AND word_wordlist_wordlist_id = ?;
WordInWordlist实体的适用部分是打击:
class WordInWordlist
{
/**
*
* @ORM\OneToMany(targetEntity="Definition", mappedBy="wordInWordlist")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="word_id", referencedColumnName="word_wordlist_word_id"),
* @ORM\JoinColumn(name="wordlist_id", referencedColumnName="word_wordlist_wordlist_id")
* })
* @Expose()
**/
protected $definitions;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Wordlist", inversedBy="wordInWordlist")
* @ORM\JoinColumn(name="wordlist_id", referencedColumnName="id")
**/
protected $wordlist;
定义实体的适用部分如下:
class Definition
{
/**
* @ORM\ManyToOne(targetEntity="WordInWordlist", inversedBy="definitions")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="word_wordlist_word_id", referencedColumnName="word_id"),
* @ORM\JoinColumn(name="word_wordlist_wordlist_id", referencedColumnName="wordlist_id")
* })
**/
protected $wordInWordlist;
public function getDefinitions($wordlist_id, $word_id)
{
$query = $this->createQueryBuilder('d')
->where('d.word_wordlist_word_id = :word_id')
->andWhere('d.word_wordlist_wordlist_id = :wordlist_id')
->setParameter('word_wordlist_word_id', $word_id)
->setParameter('word_wordlist_wordlist_id', $wordlist_id)
->getQuery();
return $query->getResult();
}
结果:
PHP Fatal error: Uncaught Doctrine\ORM\Query\QueryException: SELECT d FROM Spellingbee\Business\Entity\Definition d WHERE d.word_wordlist_word_id = :word_id AND d.word_wordlist_wordlist_id = :wordlist_id in /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:41 Stack trace:
#0 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(483): Doctrine\ORM\Query\QueryException::dqlError('SELECT d FROM S...')
#1 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(758): Doctrine\ORM\Query\Parser->semanticalError('line 0, col 63 ...', Array)
#2 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(279): Doctrine\ORM\Query\Parser->processDeferredPathExpressions(Object(Doctrine\ORM\Query\AST\SelectStatement))
#3 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(351): Doctrine\ORM\Query\Parser->getAST()
#4 /home/dporter/projects/lis in /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php on line 63
public function getDefinitions($wordlist_id, $word_id)
{
$query = $this->createQueryBuilder('d')
->where('d.wordInWordlist = :wordlist_id')
->setParameter('wordlist_id', array(
'word' => $word_id,
'wordlist' => $wordlist_id
)
)
->getQuery();
return $query->getResult();
}
结果:
Fatal error: Uncaught Doctrine\ORM\Query\QueryException: A single-valued association path expression to an entity with a composite primary key is not supported. Explicitly name the components of the composite primary key in the query. in /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:235 Stack trace:
#0 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php(676): Doctrine\ORM\Query\QueryException::associationPathCompositeKeyNotSupported()
#1 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/AST/PathExpression.php(79): Doctrine\ORM\Query\SqlWalker->walkPathExpression(Object(Doctrine\ORM\Query\AST\PathExpression))
#2 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php(2295): Doctrine\ORM\Query\AST\PathExpression->dispatch(Object(Doctrine\ORM\Query\SqlWalker))
#3 /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php in /home/dporter/projects/list_management/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php on line 235
如何创建一个学说查询以获得我在上面“我正在尝试的内容”部分中概述的结果。
由于
答案 0 :(得分:1)
您是否通过这种方式尝试,您的方法的签名是数组?
public function getDefinitions(array $ids)
{
$query = $this->createQueryBuilder('d')
->where('d.wordInWordlist IN (:wordlist_id)')
->setParameter('wordlist_id', $ids)
->getQuery();
return $query->getResult();
}
希望它可以帮到你。
答案 1 :(得分:1)
不幸的是,Doctrine不允许我尝试的查询(这看起来很基本)。使用DQL或查询构建器执行此操作的唯一方法是连接逆表并按适当的字段进行筛选。
所以这是我的最终结果:
public function getDefinitions($wordlist_id, $word_id)
{
$query = $this->createQueryBuilder('d')
->innerJoin('d.wordInWordlist', 'w')
->where('w.wordlist = :wordlist')
->andWhere('w.word = :word')
->setParameters(array(
'word' => $word_id,
'wordlist' => $wordlist_id
)
)
->getQuery();
return $query->getResult();
}