如何使用 symfony2 doctrine DQL 从多对多关系第三个 table 中获取数据
How to fetch data from many to many relation ship third table using symfony2 doctrine DQL
1)类别实体:
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="categoryname", type="string")
*/
protected $categoryname;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="categories")
*/
protected $tags;
/**
* @return ArrayCollection
*/
public function __construct(){
$this->tags = new ArrayCollection();
}
2) 标记实体:
/**
* Tag
* @ORM\Table(name="tag")
* @ORM\Entity(repositoryClass="AppBundle\Repository\TagRepository")
*/
class Tag {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
*
* @var string
*
*@ORM\Column(name="tagname", type="string")
*/
protected $tagname;
/**
* @ORM\ManyToMany(targetEntity="Category", mappedBy="tags")
*/
protected $categories;
/**
* @return ArrayCollection
*/
public function __construct(){
$this->categories = new ArrayCollection();
}
我正在使用以下 dql 查询:从数据库中获取第三个 table 数据,但我坚持获取第三个 table 数据:
$categoryId = $request->request->get('cat_id');
$repository = $em->getRepository('AppBundle:Tag');
$tags = $repository->createQueryBuilder('t')
->innerJoin('t.categories', 'c')
->where('c.id = :category_id')
->setParameter('category_id', $categoryId)
->getQuery()->getResult();
如何使用 DQl 查询从数据库中获取第三个 table(category_tag) 数据:
感谢提前...
我认为你应该在 Category 实体的 $tags 属性中添加一个 * @JoinTable 注释,如下所示:
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="categories")
* @ORM\JoinTable(name="category_tag")
*/
protected $tags;
在此处查看 Doctrine Many-toMany 的文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional。
如果您希望查询的结果是一个包含具有多对多关系的 ID 对的数组,那么您的查询应如下所示:
$tags = $repository->createQueryBuilder('t')
->select('c.id as categoryId, t.id as tagId')
->innerJoin('t.categories', 'c')
->where('c.id = :category_id')
->setParameter('category_id', $categoryId)
->getQuery()
->getResult();
希望对您有所帮助! :)
1)类别实体:
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="categoryname", type="string")
*/
protected $categoryname;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="categories")
*/
protected $tags;
/**
* @return ArrayCollection
*/
public function __construct(){
$this->tags = new ArrayCollection();
}
2) 标记实体:
/**
* Tag
* @ORM\Table(name="tag")
* @ORM\Entity(repositoryClass="AppBundle\Repository\TagRepository")
*/
class Tag {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
*
* @var string
*
*@ORM\Column(name="tagname", type="string")
*/
protected $tagname;
/**
* @ORM\ManyToMany(targetEntity="Category", mappedBy="tags")
*/
protected $categories;
/**
* @return ArrayCollection
*/
public function __construct(){
$this->categories = new ArrayCollection();
}
我正在使用以下 dql 查询:从数据库中获取第三个 table 数据,但我坚持获取第三个 table 数据:
$categoryId = $request->request->get('cat_id');
$repository = $em->getRepository('AppBundle:Tag');
$tags = $repository->createQueryBuilder('t')
->innerJoin('t.categories', 'c')
->where('c.id = :category_id')
->setParameter('category_id', $categoryId)
->getQuery()->getResult();
如何使用 DQl 查询从数据库中获取第三个 table(category_tag) 数据:
感谢提前...
我认为你应该在 Category 实体的 $tags 属性中添加一个 * @JoinTable 注释,如下所示:
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="categories")
* @ORM\JoinTable(name="category_tag")
*/
protected $tags;
在此处查看 Doctrine Many-toMany 的文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional。
如果您希望查询的结果是一个包含具有多对多关系的 ID 对的数组,那么您的查询应如下所示:
$tags = $repository->createQueryBuilder('t')
->select('c.id as categoryId, t.id as tagId')
->innerJoin('t.categories', 'c')
->where('c.id = :category_id')
->setParameter('category_id', $categoryId)
->getQuery()
->getResult();
希望对您有所帮助! :)