Symfony2 - 加入Native Query

时间:2014-03-10 16:34:41

标签: php sql symfony repository nativequery

我正在尝试使用 Symfony2 createNativeQuery 进行我的第一次加入。

我没有加入第一个,没有问题。

我认为我失去了一些步骤,以便遵循我的代码:

$rsm = new ResultSetMapping;

        $rsm
            ->addEntityResult('Art\ArticleBundle\Entity\ArticleData', 'ad')
//          ->addEntityResult('Art\ArticleBundle\Entity\Article', 'a')
//          ->addFieldResult('ad', 'id', 'article_data_id')
            ->addJoinedEntityResult('Art\ArticleBundle\Entity\Article', 'a', 'ad', 'article')
            ->addFieldResult('a', 'date_article', 'dateArticle');

        $query = $this->_em->createNativeQuery(
            'SELECT ad.id AS article_data_id, ad.title, ad.abstract '
            . 'FROM art_article_data ad '
            . 'INNER JOIN art_article ON (ad.article_id = a.id) '
            ,
            $rsm
        );

        return $query->getResult();

它返回以下错误: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'a.id' in 'on clause'")

为什么a.id未知?它在实体和表格内。 我失去了什么步骤?

2 个答案:

答案 0 :(得分:0)

正如@Javad所要求的,下面是 Article 实体

<?php

namespace Art\ArticleBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

use Art\ArticleBundle\Entity\ArticleType;
use Art\ArticleBundle\Entity\ArticleCategory;
use Art\ArticleBundle\Entity\ArticleData;
use Art\ArticleBundle\Entity\ArticleHasBrand;

/**
 * Article
 *
 * @ORM\Entity(repositoryClass="Art\ArticleBundle\Entity\Repository\ArticleRepository")
 * @ORM\Table(name="art_article", options={"collate"="utf8_general_ci", "charset"="utf8"})
 * @ORM\HasLifecycleCallbacks()
 */
class Article
{
    function __construct()
    {
        $this->articles__article_data = new ArrayCollection();
        $this->articles_article_has_brand = new ArrayCollection();
        $this->setDateInsert(new \DateTime);
        $this->setDateEdit(new \DateTime);
        $this->setIsVisible(1);
        $this->setIsMain(0);
        $this->setOrd(0);
    }

    function __toString()
    {
//      $this->dateArticle;
    }

    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer", options={"unsigned"=true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \stdClass
     *
     * @ORM\ManyToOne(targetEntity="ArticleType", inversedBy="article_types__article")
     * @ORM\JoinColumn(name="article_type_id", referencedColumnName="id", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $articleType;

    /**
     * @var \stdClass
     *
     * @ORM\ManyToOne(targetEntity="ArticleCategory", inversedBy="article_categories__article")
     * @ORM\JoinColumn(name="article_category_id", referencedColumnName="id", nullable=true)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $articleCategory;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_article", type="date", nullable=true)
     */
    private $dateArticle;

    /**
     * @var string
     *
     * @ORM\Column(name="url", type="string", length=127, nullable=true)
     */
    private $url;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_insert", type="datetime", nullable=false)
     */
    private $dateInsert;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_edit", type="datetime", nullable=true)
     */
    private $dateEdit;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_visible", type="boolean", options={"unsigned"=true, "default"=1}, nullable=false)
     */
    private $isVisible;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_main", type="boolean", options={"unsigned"=true, "default"=0}, nullable=false)
     */
    private $isMain;

    /**
     * @var integer
     *
     * @ORM\Column(name="ord", type="integer", options={"unsigned"=true, "default"=0}, nullable=true)
     */
    private $ord;

    /**
     * @var object
     * 
     * @ORM\OneToMany(targetEntity="ArticleData", mappedBy="article")
     */
    private $articles__article_data;

    /**
     * @var object
     * 
     * @ORM\OneToMany(targetEntity="ArticleHasBrand", mappedBy="article")
     */
    private $articles_article_has_brand;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set dateArticle
     *
     * @param \DateTime $dateArticle
     * @return Article
     */
    public function setDateArticle($dateArticle)
    {
        $this->dateArticle = $dateArticle;

        return $this;
    }

    /**
     * Get dateArticle
     *
     * @return \DateTime 
     */
    public function getDateArticle()
    {
        return $this->dateArticle;
    }

    /**
     * Get url
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set dateInsert
     *
     * @param string $url
     * @return Article
     */
    public function setUrl($url)
    {
        $this->url = $url;
    }

    /**
     * Set dateInsert
     *
     * @param \DateTime $dateInsert
     * @return Article
     */
    public function setDateInsert($dateInsert)
    {
        $this->dateInsert = $dateInsert;

        return $this;
    }

    /**
     * Get dateInsert
     *
     * @return \DateTime 
     */
    public function getDateInsert()
    {
        return $this->dateInsert;
    }

    /**
     * Set dateEdit
     *
     * @param \DateTime $dateEdit
     * @return Article
     */
    public function setDateEdit($dateEdit)
    {
        $this->dateEdit = $dateEdit;

        return $this;
    }

    /**
     * Get dateEdit
     *
     * @return \DateTime 
     */
    public function getDateEdit()
    {
        return $this->dateEdit;
    }

    /**
     * Set isVisible
     *
     * @param boolean $isVisible
     * @return Article
     */
    public function setIsVisible($isVisible)
    {
        $this->isVisible = $isVisible;

        return $this;
    }

    /**
     * Get isVisible
     *
     * @return boolean 
     */
    public function getIsVisible()
    {
        return $this->isVisible;
    }

    /**
     * Set isMain
     *
     * @param boolean $isMain
     * @return Article
     */
    public function setIsMain($isMain)
    {
        $this->isMain = $isMain;

        return $this;
    }

    /**
     * Get isMain
     *
     * @return boolean 
     */
    public function getIsMain()
    {
        return $this->isMain;
    }

    /**
     * Set ord
     *
     * @param integer $ord
     * @return Article
     */
    public function setOrd($ord)
    {
        $this->ord = $ord;

        return $this;
    }

    /**
     * Get ord
     *
     * @return integer 
     */
    public function getOrd()
    {
        return $this->ord;
    }

    /**
     * Set articleType
     *
     * @param \Art\ArticleBundle\Entity\ArticleType $articleType
     * @return Article
     */
    public function setArticleType(ArticleType $articleType = null)
    {
        $this->articleType = $articleType;

        return $this;
    }

    /**
     * Get articleType
     *
     * @return \Art\ArticleBundle\Entity\ArticleType 
     */
    public function getArticleType()
    {
        return $this->articleType;
    }

    /**
     * Set articleCategory
     *
     * @param \Art\ArticleBundle\Entity\ArticleCategory $articleCategory
     * @return Article
     */
    public function setArticleCategory(ArticleCategory $articleCategory = null)
    {
        $this->articleCategory = $articleCategory;

        return $this;
    }

    /**
     * Get articleCategory
     *
     * @return \Art\ArticleBundle\Entity\ArticleCategory 
     */
    public function getArticleCategory()
    {
        return $this->articleCategory;
    }

    /**
     * Add articles__article_data
     *
     * @param \Art\ArticleBundle\Entity\ArticleData $articlesArticleData
     * @return Article
     */
    public function addArticlesArticleData(ArticleData $articlesArticleData)
    {
        $this->articles__article_data[] = $articlesArticleData;

        return $this;
    }

    /**
     * Remove articles__article_data
     *
     * @param \Art\ArticleBundle\Entity\ArticleData $articlesArticleData
     */
    public function removeArticlesArticleData(ArticleData $articlesArticleData)
    {
        $this->articles__article_data->removeElement($articlesArticleData);
    }

    /**
     * Get articles__article_data
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getArticlesArticleData()
    {
        return $this->articles__article_data;
    }

    /**
     * Add articles_article_has_brand
     *
     * @param \Art\ArticleBundle\Entity\ArticleHasBrand $articlesArticleHasBrand
     * @return Article
     */
    public function addArticlesArticleHasBrand(ArticleHasBrand $articlesArticleHasBrand)
    {
        $this->articles_article_has_brand[] = $articlesArticleHasBrand;

        return $this;
    }

    /**
     * Remove articles_article_has_brand
     *
     * @param \Art\ArticleBundle\Entity\ArticleHasBrand $articlesArticleHasBrand
     */
    public function removeArticlesArticleHasBrand(ArticleHasBrand $articlesArticleHasBrand)
    {
        $this->articles_article_has_brand->removeElement($articlesArticleHasBrand);
    }

    /**
     * Get articles_article_has_brand
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getArticlesArticleHasBrand()
    {
        return $this->articles_article_has_brand;
    }
}

答案 1 :(得分:0)

您的查询应该是:

$query = $this->_em->createNativeQuery(
        'SELECT ad.id AS article_data_id, ad.title, ad.abstract '
        . 'FROM art_article_data ad '
        . 'INNER JOIN art_article a ON (ad.article_id = a.id) '
        ,
        $rsm
    );

您错过了art_article

的别名