我有表格item
与item_id, item_title, item_description, item_created, item_approved
.我还有一个带有PK item_id
(来自项目表)和article_body
的表格article
。
现在我想选择所有item.item_approved不等于 NULL 的文章。但是我坚持创建查询。这就是我现在拥有的:
$entityManager = $this->getDoctrine()->getManager();
$repository = $entityManager->getRepository('VolleyScoutBundle:Article');
$query = $repository->createQueryBuilder('a')
->where('a.item.ItemApproved != NULL')
->getQuery();
$articles = $query->getResult();
这给了我一个错误:[Syntax Error] line 0, col 73: Error: Expected =, <, <=, <>, >, >=, !=, got '.'
这是我的文章实体:
<?php
namespace VolleyScoutVolleyScoutBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Article
*
* @ORMTable(name="article")
* @ORMEntity
*/
class Article
{
/**
* @var string
*
* @ORMColumn(name="article_body", type="text", nullable=false)
*/
private $articleBody;
/**
* @var VolleyScoutVolleyScoutBundleEntityItem
*
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
* @ORMOneToOne(targetEntity="VolleyScoutVolleyScoutBundleEntityItem")
* @ORMJoinColumns({
* @ORMJoinColumn(name="item_id", referencedColumnName="item_id")
* })
*/
private $item;
/**
* Set articleBody
*
* @param string $articleBody
* @return Article
*/
public function setArticleBody($articleBody)
{
$this->articleBody = $articleBody;
return $this;
}
/**
* Get articleBody
*
* @return string
*/
public function getArticleBody()
{
return $this->articleBody;
}
/**
* Set item
*
* @param VolleyScoutVolleyScoutBundleEntityItem $item
* @return Article
*/
public function setItem(VolleyScoutVolleyScoutBundleEntityItem $item)
{
$this->item = $item;
return $this;
}
/**
* Get item
*
* @return VolleyScoutVolleyScoutBundleEntityItem
*/
public function getItem()
{
return $this->item;
}
}
这是我的项目实体:
<?php
namespace VolleyScoutVolleyScoutBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Item
*
* @ORMTable(name="item", indexes={@ORMIndex(name="fk_item_users1_idx", columns={"user_id"}), @ORMIndex(name="fk_item_myteam1_idx", columns={"myteam_id"})})
* @ORMEntity
*/
class Item
{
/**
* @var string
*
* @ORMColumn(name="item_title", type="string", length=255, nullable=false)
*/
private $itemTitle;
/**
* @var string
*
* @ORMColumn(name="item_description", type="text", nullable=false)
*/
private $itemDescription;
/**
* @var DateTime
*
* @ORMColumn(name="item_created", type="datetime", nullable=false)
*/
private $itemCreated;
/**
* @var DateTime
*
* @ORMColumn(name="item_approved", type="datetime", nullable=true)
*/
private $itemApproved;
/**
* @var DateTime
*
* @ORMColumn(name="item_deleted", type="datetime", nullable=true)
*/
private $itemDeleted;
/**
* @var integer
*
* @ORMColumn(name="item_id", type="bigint")
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $itemId;
/**
* @var VolleyScoutVolleyScoutBundleEntityMyteam
*
* @ORMManyToOne(targetEntity="VolleyScoutVolleyScoutBundleEntityMyteam")
* @ORMJoinColumns({
* @ORMJoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* @var VolleyScoutVolleyScoutBundleEntityUsers
*
* @ORMManyToOne(targetEntity="VolleyScoutVolleyScoutBundleEntityUsers")
* @ORMJoinColumns({
* @ORMJoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set itemTitle
*
* @param string $itemTitle
* @return Item
*/
public function setItemTitle($itemTitle)
{
$this->itemTitle = $itemTitle;
return $this;
}
/**
* Get itemTitle
*
* @return string
*/
public function getItemTitle()
{
return $this->itemTitle;
}
/**
* Set itemDescription
*
* @param string $itemDescription
* @return Item
*/
public function setItemDescription($itemDescription)
{
$this->itemDescription = $itemDescription;
return $this;
}
/**
* Get itemDescription
*
* @return string
*/
public function getItemDescription()
{
return $this->itemDescription;
}
/**
* Set itemCreated
*
* @param DateTime $itemCreated
* @return Item
*/
public function setItemCreated($itemCreated)
{
$this->itemCreated = $itemCreated;
return $this;
}
/**
* Get itemCreated
*
* @return DateTime
*/
public function getItemCreated()
{
return $this->itemCreated;
}
/**
* Set itemApproved
*
* @param DateTime $itemApproved
* @return Item
*/
public function setItemApproved($itemApproved)
{
$this->itemApproved = $itemApproved;
return $this;
}
/**
* Get itemApproved
*
* @return DateTime
*/
public function getItemApproved()
{
return $this->itemApproved;
}
/**
* Set itemDeleted
*
* @param DateTime $itemDeleted
* @return Item
*/
public function setItemDeleted($itemDeleted)
{
$this->itemDeleted = $itemDeleted;
return $this;
}
/**
* Get itemDeleted
*
* @return DateTime
*/
public function getItemDeleted()
{
return $this->itemDeleted;
}
/**
* Get itemId
*
* @return integer
*/
public function getItemId()
{
return $this->itemId;
}
/**
* Set myteam
*
* @param VolleyScoutVolleyScoutBundleEntityMyteam $myteam
* @return Item
*/
public function setMyteam(VolleyScoutVolleyScoutBundleEntityMyteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* @return VolleyScoutVolleyScoutBundleEntityMyteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set user
*
* @param VolleyScoutVolleyScoutBundleEntityUsers $user
* @return Item
*/
public function setUser(VolleyScoutVolleyScoutBundleEntityUsers $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return VolleyScoutVolleyScoutBundleEntityUsers
*/
public function getUser()
{
return $this->user;
}
}
尝试:
$query = $repository->createQueryBuilder('a')
->join('a.item', 'i')
->where('i.ItemApproved is not NULL')
->getQuery();