2016-08-30 1 views
1

J'ai un problème. Mon problème est ceci.Beaucoup à plusieurs doctrine symfony

ont une table: produits, catégories, products_categories

Product 
+----+--------------------------+ 
| id | title     | 
+----+--------------------------+ 
| 1 | Product 1    | 
| 2 | Product 2    | 
+----+--------------------------+ 

    Categries 
+----+----------------------+----------+ 
| id | title    | slug  | 
+----+----------------------+----------+ 
| 1 | Categories 1   | cat1  | 
| 2 | Categories 2   | cat2  | 
| 3 | Categories 3   | cat3  | 
+----+----------------------+----------+ 

    Produts_Categories 
+----------+---------------+ 
| product_id | category_id | 
+----------+---------------+ 
|  1 |  3  | 
|  2 |  3  | 
|  1 |  1  | 
+----------+---------------+ 

que je dois faire ceci:

  1. obtenir des produits de la catégorie id
  2. Obtenez tous les produits avec la catégorie

Mon entité de produit

namespace ProductBundle\Entity; 

use AppBundle\AppBundle; 
use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use AppBundle\Entity\User; 
use AppBundle\Entity\Categories; 

/** 
* Product 
* 
* @ORM\Table(name="product") 
* @ORM\Entity(repositoryClass="ProductBundle\Repository\ProductRepository") 
*/ 
class Product 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* 
* @var ArrayCollection $categories 
* 
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Categories") 
* @ORM\JoinTable(name="product_categories", 
*  joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")} 
*  ) 
*/ 
private $categories; 

/** 
* @var int 
* 
* @ORM\Column(name="user_id", type="integer") 
*/ 
private $userId; 

/** 
* 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User") 
* @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
*/ 
private $user; 

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

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

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

/** 
* @var \DateTime 
* 
* @ORM\Column(name="created_at", type="datetime") 
*/ 
private $createdAt; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="updated_at", type="datetime") 
*/ 
private $updatedAt; 

public function __construct() 
{ 
    $this->categories = new \Doctrine\Common\Collections\ArrayCollection(); 
} 


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

/** 
* Set userId 
* 
* @param integer $userId 
* @return Product 
*/ 
public function setUserId($userId) 
{ 
    $this->userId = $userId; 

    return $this; 
} 

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

/** 
* Set title 
* 
* @param string $title 
* @return Product 
*/ 
public function setTitle($title) 
{ 
    $this->title = $title; 

    return $this; 
} 

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

/** 
* Set description 
* 
* @param string $description 
* @return Product 
*/ 
public function setDescription($description) 
{ 
    $this->description = $description; 

    return $this; 
} 

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

/** 
* Set slug 
* 
* @param string $slug 
* @return Product 
*/ 
public function setSlug($slug) 
{ 
    $this->slug = $slug; 

    return $this; 
} 

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

/** 
* Set createdAt 
* 
* @param \DateTime $createdAt 
* @return Product 
*/ 
public function setCreatedAt($createdAt) 
{ 
    $this->createdAt = $createdAt; 

    return $this; 
} 

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

/** 
* Set updatedAt 
* 
* @param \DateTime $updatedAt 
* @return Product 
*/ 
public function setUpdatedAt($updatedAt) 
{ 
    $this->updatedAt = $updatedAt; 

    return $this; 
} 

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

/** 
* @return User 
* 
*/ 
public function getUser() 
{ 
    return $this->user; 
} 

/** 
* @param User $user 
*/ 
public function setUser(\AppBundle\Entity\User $user) 
{ 
    $this->user = $user; 
} 

/** 
* Add categories 
* 
* @param \AppBundle\Entity\Categories $categories 
* @return Product 
*/ 
public function addCategory(\AppBundle\Entity\Categories $categories) 
{ 
    $this->categories[] = $categories; 

    return $this; 
} 

/** 
* Remove categories 
* 
* @param \AppBundle\Entity\Categories $categories 
*/ 
public function removeCategory(\AppBundle\Entity\Categories $categories) 
{ 
    $this->categories->removeElement($categories); 
} 

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

Mes catégories Entité

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Categories 
* 
* @ORM\Table(name="categories") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoriesRepository") 
*/ 
class Categories 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var int 
* 
* @ORM\Column(name="parent", type="integer") 
*/ 
private $parent; 

/** 
* @var int 
* 
* @ORM\Column(name="pos", type="integer") 
*/ 
private $pos; 

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

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


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

/** 
* Set parent 
* 
* @param integer $parent 
* @return Categories 
*/ 
public function setParent($parent) 
{ 
    $this->parent = $parent; 

    return $this; 
} 

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

/** 
* Set pos 
* 
* @param integer $pos 
* @return Categories 
*/ 
public function setPos($pos) 
{ 
    $this->pos = $pos; 

    return $this; 
} 

/** 
* Get parent 
* 
* @return integer 
*/ 
public function getPos() 
{ 
    return $this->pos; 
} 

/** 
* Set title 
* 
* @param string $title 
* @return Categories 
*/ 
public function setTitle($title) 
{ 
    $this->title = $title; 

    return $this; 
} 

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

/** 
* Set slug 
* 
* @param string $slug 
* @return Categories 
*/ 
public function setSlug($slug) 
{ 
    $this->slug = $slug; 

    return $this; 
} 

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

ProductController

$em = $this->container->get('doctrine')->getManager(); 
$repository = $em->getRepository('ProductBundle:Product'); 
$products = $repository 
->findAll(); 

Mais cela ne fonctionne pas. Je reçois ceci:

array:2 [▼ 
    0 => Product {#675 ▼ 
    -id: 1 
    -categories: PersistentCollection {#676 ▼ 
     -snapshot: [] 
     -owner: Product {#675} 
     -association: array:19 [ …19] 
     -em: EntityManager {#574 …10} 
     -backRefFieldName: null 
     -typeClass: ClassMetadata {#583 …} 
     -isDirty: false 
     -initialized: false 
     -coll: ArrayCollection {#677 ▶} 
    } 
    -userId: 1 
    -user: User {#696 ▶} 
    -title: "hjhkhk" 
    -description: "hjhkjhkjh" 
    -slug: "hjhkjhkj" 
    -createdAt: DateTime {#672 ▶} 
    -updatedAt: DateTime {#673 ▶} 
    } 
    1 => Product {#693 ▶} 
] 
+0

Et? Qu'attendez-vous? "Obtenir tous les produits avec la catégorie" ne dit pas grand chose. Vos produits semblent avoir une certaine catégorie. – mmmm

+0

Je veux obtenir les produits par identifiant de catégorie. Et, le tableau de produits pour avoir des informations sur les catégories associées au produit (nom, identifiant et etc.) –

Répondre

0

1ère approche :

public function findProductsByCategory($categoryId) { 
    $qb = $this->createQueryBuilder('p'); 
    $qb->select('p, cat') 
     ->leftJoin('p.categories', 'cat') 
     ->where('cat.id = :catId') 
     ->setParameter('catId', $categoryId); 
    return $qb->getQuery()->getResult(); 
} 

Cette requête renverra les produits pour la catégorie donnée. Si vous souhaitez recevoir des catégories pour chaque produit, vous devrez parcourir les produits et obtenir des catégories pour chacun d'entre eux.

2ème approche:

public function getProductsWithCategories() { 
    $qb = $this->createQueryBuilder('p'); 
    $qb->select('p, cat') 
     ->leftJoin('p.categories', 'cat'); 
    return $qb->getQuery()->getResult(); 
} 

Cette requête renverra tous les produits avec des catégories jointes. Maintenant, vous devez filtrer dans le code, ces produits qui vous intéressent

La différence:.

En 1ère approche Vous enverrez une requête en demandant la catégorie pour chaque produit.

Dans la deuxième approche, le code est responsable du filtrage des produits. Donc, fondamentalement, cela dépend si vous voulez DB ou code pour gérer plus de charge.

+0

Very thanx mmmm! :) C'est du travail! –

+0

Dans cette fonction findProductsByCategory(), je reçois des informations sur une catégorie que j'ai choisie. Mais ce produit a 2 catégories différentes. Comment le réparer? –

+0

être spécifique. Qu'est-ce que cela signifie que vous obtenez des informations sur une catégorie. – mmmm

0

Vous pourriez faire votre propre demande dans le référentiel avec vos propres logiques (comme il semble tout à fait clair ce que vous voulez exactement), tels que:

public function findProducts() { 
    $qb = $this->createQueryBuilder('p'); 
    $qb ->leftJoin('p.categories', 'cat'); 
    return $qb->getQuery()->getArrayResult(); 
}