2016-08-30 217 views
1

我有一个问题。我的问题是这样的。多对多教条symfony

有一个表:产品,类别,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  | 
+----------+---------------+ 

我需要这样做:

  1. 获得从类别ID产品
  2. 获取所有产品类别

我的产品实体

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; 
} 
} 

我的类别实体

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(); 

但它不工作。我得到这个:

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

而且?你期望得到什么? “获得所有具有类别的产品”并不多。你的产品似乎有一些类别。 – mmmm

+0

我想按类别id获取产品。并且,该产品阵列具有关于与产品相关的类别(名称,ID等)的信息 –

回答

0

1的方法:

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(); 
} 

此查询将返回给定类别的产品。如果您想要接收每个产品的类别,则必须遍历产品并获取每个类别的类别。

第二个办法:

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

这个查询将返回所有产品类别的加盟。现在,你必须在代码中过滤掉这些产品,你有兴趣

的区别:

在第一次的方法,请求类别为每个产品时,您会发送一个查询。

在第二种方法中,代码负责过滤产品。所以基本上取决于你是想要数据库还是代码来处理更多的负载。

+0

Very thanx mmmm! :) 是工作! –

+0

在这个函数findProductsByCategory()我得到关于一个类别,我选择的信息。但是这个产品有两个不同的类别。如何解决它? –

+0

具体。这是什么意思,你得到关于一个类别的信息。 – mmmm

0

你可能做到用自己的逻辑存储库您自己的要求(因为它似乎还不是很清楚你想要什么),如:

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