我通过终端在mysql中尝试这个
select doc.id from Document doc where doc.id not in (select ck.document_id from CheckDoc ck where ck.requestItem_id = 7);
它成功地工作。
但是当我在 symfony 中使用它时,它不起作用。我收到此错误:
[Semantical Error] line 0, col 148 near 'doc_id':
Error: Class FWZ\RequestBundle\Entity\CheckDoc has no field or association named doc_id
我的桌子:
文档表:通过 CheckDoc 表的 id 、 title 和 OnetoMany 关系
/**
* @ORM\OneToMany(targetEntity="CheckDoc", mappedBy="document" )
*/
protected $checkdoc;
public function __construct()
{
$this->checkdoc = new ArrayCollection();
}
/**
* Add checkdoc
*
* @param FWZ\RequestBundle\Entity\CheckDoc $checkdoc
*/
public function addCheckdoc(\FWZ\RequestBundle\Entity\CheckDoc $checkdoc)
{
$this->checkdoc[] = $checkdoc;
}
/**
* Get checkdoc
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCheckdoc()
{
return $this->checkdoc;
}
RequestItem 表:通过 CheckDoc 表的 id 、 title 和 OneToMany 关系
/**
* @ORM\OneToMany(targetEntity="CheckDoc", mappedBy="request" )
*/
protected $checkdoc;
public function __construct()
{
$this->checkdoc = new ArrayCollection();
}
/**
* Add checkdoc
*
* @param FWZ\RequestBundle\Entity\CheckDoc $checkdoc
*/
public function addCheckdoc(\FWZ\RequestBundle\Entity\CheckDoc $checkdoc)
{
$this->checkdoc[] = $checkdoc;
}
/**
* Get checkdoc
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCheckdoc()
{
return $this->checkdoc;
}
CheckDoc 表 id、title、ManyToOne 与 Document 和 RequestItem 表的关系
/**
* @ORM\ManyToOne(targetEntity="RequestItem", inversedBy="checkdoc" )
* @ORM\JoinColumn(name="requestItem_id", referencedColumnName="id")
*/
protected $request;
/**
* @ORM\ManyToOne(targetEntity="Document", inversedBy="checkdoc" )
* @ORM\JoinColumn(name="doc_id", referencedColumnName="id")
*/
protected $document;
/**
* Set request
*
* @param FWZ\RequestBundle\Entity\RequestItem $request
*/
public function setRequest(\FWZ\RequestBundle\Entity\RequestItem $request)
{
$this->request = $request;
}
/**
* Get request
*
* @return FWZ\RequestBundle\Entity\RequestItem
*/
public function getRequest()
{
return $this->request;
}
/**
* Set document
*
* @param FWZ\RequestBundle\Entity\Document $document
*/
public function setDocument(\FWZ\RequestBundle\Entity\Document $document)
{
$this->document = $document;
}
/**
* Get document
*
* @return FWZ\RequestBundle\Entity\Document
*/
public function getDocument()
{
return $this->document;
}
和我在控制器中的查询
$query = $em->createQuery(
'SELECT doc.id
FROM FWZRequestBundle:Document doc
WHERE doc.id not in (
select ck.doc_id
from FWZRequestBundle:CheckDoc ck
where ck.requestItem_id = 7 )'
);
$documents = $query->getResult();