使用查询生成器从关系 table 中获取信息
Use query builder to get informations from relation table
我有两个实体,Annonce 和 Category 具有多对多关系。
我想获得类别包含在数组中的通知。
在数据库中我有以下架构:
Category Table :
Annonce table:
Annonce_Category table:
这是我的查询生成器:
$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 中,我有与我的公告关联的每个类别的名称 (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 中的类别的通知。
编辑
$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();
可是我还是没有结果
也许会改变
->leftJoin('MDBAnnonceBundle:Category', 'c', 'WITH', 'c.id = a.id')
至
->innerJoin('a.categories', 'c')
因为您获取了所有公告,而不管它们的类别...
我有两个实体,Annonce 和 Category 具有多对多关系。 我想获得类别包含在数组中的通知。
在数据库中我有以下架构:
Category Table :
Annonce table:
Annonce_Category table:
这是我的查询生成器:
$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 中,我有与我的公告关联的每个类别的名称 (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 中的类别的通知。
编辑
$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();
可是我还是没有结果
也许会改变
->leftJoin('MDBAnnonceBundle:Category', 'c', 'WITH', 'c.id = a.id')
至
->innerJoin('a.categories', 'c')
因为您获取了所有公告,而不管它们的类别...