现在我试着绕着Doctrine2。
所以我有以下结构:
我有一个文章实体:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="article")
*/
class Article extends BaseEntity {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="ArticleRevision", mappedBy="article", cascade={"persist"})
* @var ArrayCollection
*/
private $articleRevisions;
/**
* @ORM\ManyToOne(targetEntity="ArticleRevision")
* @ORM\JoinColumn(name="currentRevisionId", referencedColumnName="id", nullable=true)
* @var ArticleRevision
*/
private $currentRevision;
public function __construct(array $options = null) {
$this->articleRevisions = new ArrayCollection();
parent::__construct($options);
}
/**
* @return int
*/
public function getId() {
return $this->id;
}
/**
* @param int $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param ArticleRevision $articleRevision
*/
public function addArticleRevision($articleRevision) {
$this->articleRevisions[] = $articleRevision;
$articleRevision->setArticle($this);
}
/**
* @return ArrayCollection
*/
public function getArticleRevisions() {
return $this->articleRevisions;
}
/**
* @return ArticleRevision
*/
public function getCurrentRevision() {
return $this->currentRevision;
}
/**
* @param ArticleRevision $articleRevision
*/
public function setCurrentRevision($articleRevision) {
$this->currentRevision = $articleRevision;
}
}
有articleRevisions并引用其当前版本:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="articlerevision")
*/
class ArticleRevision extends BaseEntity {
const TYPE_RELEASE = "release";
const TYPE_DRAFT = "draft";
const TYPE_AUTOSAVE = "autosave";
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @var int
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Category")
* @ORM\JoinColumn(name="categoryId", referencedColumnName="id")
* @var Category
**/
private $category;
/**
* @ORM\ManyToOne(targetEntity="Article", inversedBy="articleRevisions")
* @ORM\JoinColumn(name="articleId", referencedColumnName="id")
* @var Article
**/
private $article;
/**
* @ORM\Column(type="string")
* @var string
*/
protected $urlParam;
/**
* @ORM\Column(type="string")
* @var string
*/
protected $copyright;
/**
* @ORM\Column(type="string")
* @var string
*/
protected $supertitle;
/**
* @ORM\Column(type="string")
* @var string
*/
protected $title;
/**
* @ORM\Column(type="text")
* @var string
*/
protected $teaser;
/**
* @ORM\Column(type="text")
* @var string
*/
protected $text;
/**
* @ORM\ManyToOne(targetEntity="ArticleRevision")
* @ORM\JoinColumn(name="previousRevisionId", referencedColumnName="id", nullable=true)
* @var ArticleRevision
*/
private $previousRevision;
public function __construct(array $options = null) {
parent::__construct($options);
}
/**
* @return int
*/
public function getId() {
return $this->id;
}
/**
* @param int $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @return Category
*/
public function getCategory() {
return $this->category;
}
/**
* @param Category $category
*/
public function setCategory($category){
$this->category = $category;
}
/**
* @return Article
*/
public function getArticle() {
return $this->article;
}
/**
* @param Article $article
*/
public function setArticle($article) {
$this->article = $article;
}
/**
* @return string
*/
public function getCopyright() {
return $this->copyright;
}
/**
* @param string $copyright
*/
public function setCopyright($copyright) {
$this->copyright = $copyright;
}
/**
* @return string
*/
public function getUrlParam() {
return $this->urlParam;
}
/**
* @param string $urlParam
*/
public function setUrlParam($urlParam) {
$this->urlParam = $urlParam;
}
/**
* @return string
*/
public function getSupertitle() {
return $this->supertitle;
}
/**
* @param string $supertitle
*/
public function setSupertitle($supertitle) {
$this->supertitle = $supertitle;
}
/**
* @return string
*/
public function getTitle() {
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title) {
$this->title = $title;
}
/**
* @return string
*/
public function getTeaser() {
return $this->teaser;
}
/**
* @param string $teaser
*/
public function setTeaser($teaser) {
$this->teaser = $teaser;
}
/**
* @return string
*/
public function getText() {
return $this->text;
}
/**
* @param string $text
*/
public function setText($text) {
$this->text = $text;
}
/**
* @return ArticleRevision
*/
public function getPreviousRevision() {
return $this->previousRevision;
}
/**
* @param ArticleRevision $previousRevision
*/
public function setPreviousRevision($previousRevision) {
$this->previousRevision = $previousRevision;
}
}
我想要实现的是选择每篇文章,其中currentRevision的categoryId例如是1。
我试过了:
$qb = $this->getRepository()->createQueryBuilder('a');
$qb->leftJoin('a.currentRevision', 'r')
->where('r.category = :category')
->setParameters(array('category' => $category));
return $qb->getQuery()->getResult();
其中$category
是int或Application\Entity\Category
。两者都返回一个空数组。你能指出我正确的方向吗?
感谢任何帮助!
修改
$qb->__toString()
的输出:
选择一个FROM Application \ Entity \ Article a LEFT JOIN a.currentRevision r WHERE r.category =:category
答案 0 :(得分:2)
r.category =:category,对实体类别的引用而不是标量字段,您还应该加入Category并将条件更改为类似的内容:
$qb = $this->getRepository()->createQueryBuilder('a');
$qb->leftJoin('a.currentRevision', 'r')
->leftJoin('r.category', 'c')
->where('c.id = :category')
->setParameters(array('category' => $category));
return $qb->getQuery()->getResult();