【问题标题】:How to join many to many in createQuery()如何在 createQuery() 中加入多对多
【发布时间】:2015-03-15 03:13:46
【问题描述】:

我正在处理我的作品集,并且我有一个展示我的项目的页面。我现在创建了 2 个 cayegorie:编程和艺术。我的每个项目都可以是编程项目、艺术项目或两者兼而有之。为此,我制作了一个表格项目和一个表格类别,它们以多对多关系连接。到目前为止,没问题。

我开始在我的项目实体的存储库文件中创建我的查询。问题是当我尝试加入我的项目实体和我的类别实体时,它看起来不起作用,因为我指定我只想要至少对类别进行编程的项目。但是当它应该只给我其中的两个时,它仍然返回给我所有的项目。

我的 JOIN 是否正确?

以下是实体(注意法语!): 项目:

<?php

namespace PublicBundle\Entity;

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

/**
 * Projet
 *
 * @ORM\Table(name="pt_projet");
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetDepot")
 */
class Projet
{

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    //ID du projet
    protected $id;

    /**
     * @ORM\Column(name="pro_tag", type="string",length=255, unique=true)
     */
    //Tag du projet
    protected $tag;

     /**
     * @ORM\OneToMany(targetEntity="ProjetInt", mappedBy="projetId", orphanRemoval=true)
     */
    protected $descriptions;

    /**
     * @ORM\Column(name="pro_img", type="string", length=64, unique=true)
     */
    //Nom du fichier de l'image du projet
    protected $image;

    /**
     * @ORM\Column(name="pro_technologie_utilisee", type="text", length=200)
     */
    //Text qui liste tout les technologies utilisées pour le projet
    protected $technologie;

    /**
     * @ORM\Column(name="pro_annee", type="integer", length=4)
     */
    //Année de réalisation du projet
    protected $annee;

    /**
     * @ORM\ManyToOne(targetEntity="Type", inversedBy="projets")
     * @ORM\JoinColumn(name="pro_type", referencedColumnName="id", nullable=false)
     */
    //Clef étrangère du type de projet
    //Le type de projet ne correspond pas à la catégore. Il peu être Unity, flash, image, vidéo, etc. Il permet de savoir quelle page charger pour pouvoir intégrer le projet dans le portfolio.
    protected $type;

    /**
     * @ORM\Column(name="pro_fichier", type="string", length=64, unique=true)
     */
    //Nom du fichier du projet
    private $fichier;

    /**
    * @ORM\ManyToMany(targetEntity="Categorie", cascade={"persist"})
    */
    //La ou les catégories du projet
    private $categories;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->descriptions=new ArrayCollection();
        $this->categories=new ArrayCollection();
    }

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

    /**
     * Set image
     *
     * @param string $image
     * @return Projet
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

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

    /**
     * Set technologie
     *
     * @param string $technologie
     * @return Projet
     */
    public function setTechnologie($technologie)
    {
        $this->technologie = $technologie;

        return $this;
    }

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

    /**
     * Set annee
     *
     * @param integer $annee
     * @return Projet
     */
    public function setAnnee($annee)
    {
        $this->annee = $annee;

        return $this;
    }

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

    /**
     * Set fichier
     *
     * @param string $fichier
     * @return Projet
     */
    public function setFichier($fichier)
    {
        $this->fichier = $fichier;

        return $this;
    }

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

    /**
     * Set type
     *
     * @param Type $type
     * @return Projet
     */
    public function setType(Type $type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return Type 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Add categories
     *
     * @param Categorie $categories
     * @return Projet
     */
    public function addCategory(Categorie $categories)
    {
        $this->categories[] = $categories;

        return $this;
    }

    /**
     * Remove categories
     *
     * @param Categorie $categories
     */
    public function removeCategory(Categorie $categories)
    {
        $this->categories->removeElement($categories);
    }

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


    /**
     * Add description
     *
     * @param \PublicBundle\Entity\ProjetInt $description
     * @return Projet
     */
    public function addDescription(\PublicBundle\Entity\ProjetInt $description)
    {
        $this->description[] = $description;

        return $this;
    }

    /**
     * Remove description
     *
     * @param \PublicBundle\Entity\ProjetInt $description
     */
    public function removeDescription(\PublicBundle\Entity\ProjetInt $description)
    {
        $this->description->removeElement($description);
    }

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

类别:

<?php

namespace PublicBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Catégorie
 *
 * @ORM\Table(name="pt_categorie");
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="PublicBundle\Entity\CategorieDepot")
 */
class Categorie
{

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    //ID de la catégorie
    protected $id;

    /**
     * @ORM\Column(name="cat_tag", type="string",length=255, unique=true)
     */
    //Tag de la catégorie
    protected $tag;


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

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

        return $this;
    }

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

还有仓库:

<?php

namespace PublicBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * ProjetDepot
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ProjetDepot extends EntityRepository
{

    public function rechercherProjets($lang, $cat)
    {

        return $this->getEntityManager()->createQuery(
            'SELECT p.tag,
                    p.image,
                    pi.nom,
                    pi.descriptionCours,
                    pi.descriptionComplete,
                    pi.roles,
                    p.technologie,
                    pi.aptitudesDeveloppees,
                    p.annee
            FROM PublicBundle:Projet p
            JOIN PublicBundle:ProjetInt pi
            WITH p.id=pi.projetId
            JOIN PublicBundle:Categorie c
            WHERE pi.langue=:lang
            AND c.tag=:cat'
        )->setParameters(array('lang'=>$lang,'cat'=>$cat))->getResult();

    }

}

【问题讨论】:

    标签: symfony join doctrine many-to-many createquery


    【解决方案1】:

    试试这个

    public function rechercherProjets($lang, $cat) { 
        $qb = $this->createQueryBuilder('p')
             ->innerJoin ('p.description', 'pi') 
             ->innerJoin('p.categories', 'pc')
             ->andWhere('pc.tag = :cat')
             ->andWhere('pi.langue = :lang')
             ->setParameters(array('lang'=>$lang,'cat'=>$cat));
    
         return $qb->getQuery()->getResult() 
    }
    

    【讨论】:

    • 它似乎有效,但我有几个问题。首先,有没有办法执行相同的请求,但使用 createQueryBuilder 的 createQuery?其次,当我尝试访问我的项目的标签时,它不想让我 (projet["tag"]) 我在我的树枝中使用了一个 foreach ({% for projet in projets %}) 。我发现让它工作的唯一方法是 wright->select('p.tag AS tag')
    • ^忘记关于标签的问题,我发现我的错误,但我得到了一个新的,如何访问我的 porjetint 实体中的“nom”?例如,我使用 {{ projet.tag }} 来获取标签,但是如何获取“nom”?
    • 你好。我认为使用 QueryBuilder 是更好的方法......是的......你可以做同样的请求......例如你可以打印 SQL 查询 $qb->getQuery()->getSql();
    • 你的意思是“名义”?我不明白
    • "nom" 只是我的项目实体中一个字段的名称。我只是想知道如何访问它,因为我不能做 {{ projet.nom }}
    猜你喜欢
    • 2018-03-13
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2020-09-11
    相关资源
    最近更新 更多