我有两个实体,Annonce和具有ManyToMany关系的Category。 我想得到一个包含在数组中的类别的annonce。
在数据库中,我有以下架构:
类别表:
Annonce表:
Annonce_Category表:
这是我的QueryBuilder:
$qb = $this->createQueryBuilder('a')
->select('a')
->leftJoin('a.ville', 'v')
->leftJoin('MDBAnnonceBundle:Category', 'c', 'WITH', 'c.id = a.id')
->where("c.nom IN(:arrayCat)")
->setParameter('arrayCat', $arrayCat);
if ($item != "all") {
$qb->where($qb->expr()->like('a.titre', ':titre'))
->setParameter('titre', '%' . $item . '%');
}
if ($lieu != "all") {
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->like('v.cp', ':lieu'), $qb->expr()->like('v.nomComplet', ':lieu')
)
)
->setParameter('lieu', $lieu . '%');
}
$qb->distinct()
->getQuery()
->getResult();
return $qb->getQuery()->getResult();
在$ arrayCat中,我有与我的annonce关联的每个类别的名称(Category.nom):
array (size=4)
0 => string 'Voiture' (length=7)
1 => string 'PC portable' (length=11)
2 => string 'Accesoires' (length=10)
3 => string 'Smartphone' (length=10)
这是我的实体:
Annonce.php
<?php
namespace MDB\AnnonceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Annonce
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="MDB\AnnonceBundle\Entity\AnnonceRepository")
*/
class Annonce {
public function __construct() {
$this->date = new \Datetime();
$this->categories = new ArrayCollection();
$this->images= new ArrayCollection();
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @ORM\Column(name="date", type="date")
*/
private $date;
/**
* @var float
*
* @ORM\Column(name="prix", type="float")
*/
private $prix;
/**
* @ORM\ManyToOne(targetEntity="MDB\AdresseBundle\Entity\Ville", inversedBy="annonces")
*
*/
private $ville;
/**
* @ORM\ManyToMany(targetEntity="MDB\AnnonceBundle\Entity\Category", cascade={"persist"})
*/
private $categories;
/**
* @ORM\ManyToOne(targetEntity="MDB\AnnonceBundle\Entity\AdvertType", inversedBy="annonces")
*
*/
private $advertType;
/**
* @ORM\ManyToMany(targetEntity="MDB\UserBundle\Entity\User")
*
*/
private $wishlist;
/**
* @var boolean
*
* @ORM\Column(name="telAppear", type="boolean")
*/
private $telAppear;
/**
* @ORM\ManyToOne(targetEntity="MDB\UserBundle\Entity\User", inversedBy="annonces")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\OneToMany(targetEntity="MDB\PlatformBundle\Entity\Image", mappedBy="annonce" , cascade={"persist"} )
*/
private $images;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set titre
*
* @param string $titre
* @return Annonce
*/
public function setTitre($titre) {
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* @return string
*/
public function getTitre() {
return $this->titre;
}
/**
* Set description
*
* @param string $description
* @return Annonce
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* Set prix
*
* @param float $prix
* @return Annonce
*/
public function setPrix($prix) {
$this->prix = $prix;
return $this;
}
/**
* Get prix
*
* @return float
*/
public function getPrix() {
return $this->prix;
}
public function addCategory(Category $category) {
// Ici, on utilise l'ArrayCollection vraiment comme un tableau
$this->categories[] = $category;
return $this;
}
public function removeCategory(Category $category) {
$this->categories->removeElement($category);
}
public function getCategories() {
return $this->categories;
}
public function getDate() {
return $this->date;
}
public function setDate($date) {
$this->date = $date;
}
public function getWishlist() {
return $this->wishlist;
}
public function setWishlist($wishlist) {
$this->wishlist = $wishlist;
}
public function getVille() {
return $this->ville;
}
public function setVille($ville) {
$this->ville = $ville;
}
public function getTelAppear() {
return $this->telAppear;
}
public function setTelAppear($telAppear) {
$this->telAppear = $telAppear;
}
public function getUser() {
return $this->user;
}
public function setUser($user) {
$this->user = $user;
}
public function addImage(\MDB\PlatformBundle\Entity\Image $image) {
$this->images[] = $image;
$image->upload();
return $this;
}
public function removeImage(\MDB\PlatformBundle\Entity\Image $image) {
$this->images->removeElement($image);
}
public function getImages() {
return $this->images;
}
public function getAdvertType() {
return $this->advertType;
}
public function setAdvertType($advertType) {
$this->advertType = $advertType;
}
public function getFirstImage(){
return $this->images[0];
}
}
Category.php
<?php
namespace MDB\AnnonceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* @ORM\Table()
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="appartenance", type="string", length=255)
*/
private $appartenance;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return Category
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
public function getAppartenance() {
return $this->appartenance;
}
public function setAppartenance($appartenance) {
$this->appartenance = $appartenance;
}
}
那么如何添加该子句以获得所有包含arrayCat类别的annonces。
修改
$arrayCat = explode(",", $categorie);
$qb = $this->createQueryBuilder('a')
->select('a')
->leftJoin('a.ville', 'v')
->innerJoin('MDBAnnonceBundle:Category', 'c', 'WITH', 'c.id = a.id')
->where("c.nom IN(:cat)")
->setParameter('cat', $arrayCat);
if ($item != "all") {
$qb->andWhere($qb->expr()->like('a.titre', ':titre'))
->setParameter('titre', '%' . $item . '%');
}
if ($lieu != "all") {
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->like('v.cp', ':lieu'), $qb->expr()->like('v.nomComplet', ':lieu')
)
)
->setParameter('lieu', $lieu . '%');
}
$qb->distinct()
->getQuery()
->getResult();
return $qb->getQuery()->getResult();
但我仍然没有结果
答案 0 :(得分:1)
也许改变
->leftJoin('MDBAnnonceBundle:Category', 'c', 'WITH', 'c.id = a.id')
到
->innerJoin('a.categories', 'c')
因为无论其类别如何,都会获取所有annonces ...