尝试使用doctrine queryBuilder时出现问题

时间:2016-08-09 08:44:39

标签: php symfony doctrine-orm doctrine

我有两个实体CategorieChampCat ManyToMany关系,我想通过使用doctrine QueryBuilder获取相关ChampCat的类别列表,所以我使用了这个查询:

class CategorieRepository extends \Doctrine\ORM\EntityRepository
{
    public function myFindCategories(array $tab){

        $em = $this->getEntityManager();

        $query = $em->select(array('cat', 'ch'))
                    ->from('AnnonceBundle\Entity\Categorie', 'cat')
                    ->join('cat.champsCat', 'ch')
                    ->where('cat.id In (:tabOfIds)')
                    ->setParameter('tabOfIds', array_values($tab))
                    ->getQuery();

        return $query->getResult();

    }


}

在这里你可以看到分类实体:

<?php

namespace AnnonceBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Categorie
 *
 * @ORM\Table(name="categorie")
 * @ORM\Entity(repositoryClass="AnnonceBundle\Repository\CategorieRepository")
 */
class Categorie implements \JsonSerializable
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="refCat", type="string", length=100)
     */
    private $refCat;

    /**
     * @var string
     *
     * @ORM\Column(name="libelleCat", type="string", length=50)
     */
    private $libelleCat;

    /**
     * @ORM\ManyToMany(targetEntity="AnnonceBundle\Entity\ChampCat",cascade={"persist"})
     */
    private $champsCat;

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

    /**
     * Set refCat
     *
     * @param string $refCat
     *
     * @return Categorie
     */
    public function setRefCat($refCat)
    {
        $this->refCat = $refCat;

        return $this;
    }

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

    /**
     * Set libelleCat
     *
     * @param string $libelleCat
     *
     * @return Categorie
     */
    public function setLibelleCat($libelleCat)
    {
        $this->libelleCat = $libelleCat;

        return $this;
    }

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

    /**
     * Set champsCat
     *
     * @param \AnnonceBundle\Entity\ChampCat $champsCat
     *
     * @return Categorie
     */
    public function setChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat = null)
    {
        $this->champsCat = $champsCat;

        return $this;
    }

    /**
     * Get champsCat
     *
     * @return \AnnonceBundle\Entity\ChampCat
     */
    public function getChampsCat()
    {
        return $this->champsCat;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->champsCat = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add champsCat
     *
     * @param \AnnonceBundle\Entity\ChampCat $champsCat
     *
     * @return Categorie
     */
    public function addChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat)
    {
        $this->champsCat[] = $champsCat;

        return $this;
    }

    /**
     * Remove champsCat
     *
     * @param \AnnonceBundle\Entity\ChampCat $champsCat
     */
    public function removeChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat)
    {
        $this->champsCat->removeElement($champsCat);
    }

    function jsonSerialize()
    {
        return get_object_vars($this);
    }
}

这是ChamCat实体:

<?php

namespace AnnonceBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ChampCat
 *
 * @ORM\Table(name="champ_cat")
 * @ORM\Entity(repositoryClass="AnnonceBundle\Repository\ChampCatRepository")
 */
class ChampCat implements \JsonSerializable
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     *@var string
     *
     * @ORM\Column(name="refCh", type="string")
     */
    private $refCh;



    /**
     * @var string
     *
     * @ORM\Column(name="nom_ch", type="string", length=255)
     */
    private $nomCh;

    /**
     * @var bool
     *
     * @ORM\Column(name="app_ch", type="boolean")
     */
    private $appCh;

    /**
     * @var string
     *
     * @ORM\Column(name="format_ch", type="string", length=255)
     */
    private $formatCh;


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

    /**
     * Set refCh
     *
     * @param integer $refCh
     * @return ChampCat
     */
    public function setRefCh($refCh)
    {
        $this->refCh = $refCh;

        return $this;
    }

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

    /**
     * Set nomCh
     *
     * @param string $nomCh
     * @return ChampCat
     */
    public function setNomCh($nomCh)
    {
        $this->nomCh = $nomCh;

        return $this;
    }

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

    /**
     * Set appCh
     *
     * @param boolean $appCh
     * @return ChampCat
     */
    public function setAppCh($appCh)
    {
        $this->appCh = $appCh;

        return $this;
    }

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

    /**
     * Set formatCh
     *
     * @param string $formatCh
     * @return ChampCat
     */
    public function setFormatCh($formatCh)
    {
        $this->formatCh = $formatCh;

        return $this;
    }

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

    function jsonSerialize()
    {
        return get_object_vars($this);
    }
}

不幸的是,这个查询没有用,所以我错过了什么?

1 个答案:

答案 0 :(得分:0)

是否使用此语法?

public function myFindCategories(array $tab){

    if(count($tab) == 0) return []; //or IN() will bug with an empty array


    $query = $this->createQueryBuilder('cat')
                ->addSelect('ch')
                ->innerJoin('cat.champsCat', 'ch')
                ->where('cat.id in (:tabOfIds)')
                ->setParameter('tabOfIds', $tab)
                ->getQuery();

    return $query->getResult();

}