使用2层查询时没有命名的字段或关联

时间:2013-11-08 16:18:30

标签: mysql symfony

我在终端

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

1 个答案:

答案 0 :(得分:0)

将您的查询更改为:

 $query = $em->createQuery(
                    'SELECT doc.id 
                    FROM FWZRequestBundle:Document doc 
                    WHERE doc.id  not in (
                                    select ck.document.doc_id 
                                    from FWZRequestBundle:CheckDoc ck
                                    where ck.request.requestItem_id = 7 )'
                    );           

     $documents = $query->getResult();

这只是一个错字:您使用doc_id代替document_id,就像您向我们展示的正常运行的查询一样。