gpt4 book ai didi

php - 如何查找实体 ManyToMany

转载 作者:行者123 更新时间:2023-11-30 22:39:29 28 4
gpt4 key购买 nike

我有两个实体 Post 和 Tags 这个实体有 ManyToMany 的关系,很多帖子有很多标签,很多标签有很多帖子,并且有实体类别我有一个类别的很多帖子,我需要创建 Action 来查找类别的帖子并找到帖子对于标签

class Tag
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="string", length=100)
*/
protected $hashTag;

/**
* @Gedmo\Slug(fields={"hashTag"})
* @ORM\Column(type="string", length=12)
*/
protected $hashSlug;

/**
* @ORM\ManyToMany(targetEntity="Post", inversedBy="tag")
* @ORM\JoinColumn(name="post_id", referencedColumnName="id")
*/
protected $post;

类别:

class Category
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;

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

/**
* @ORM\OneToMany(targetEntity="Post", mappedBy="category")
*/
protected $posts;

发布

class Post
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\Column(type="string", nullable=true)
*/
private $title;
/**
* @ORM\ManyToMany(targetEntity="Tag", mappedBy="post")
* @ORM\JoinTable(name="posts_tag")
*/
protected $tag;

以及我在操作字段 hashTag 中发布的模板:

    <p class="lead">
Category:<a href="{{ path('get_posts_for_category', {'category': post.category.title}) }}">{{ post.category }}</a>
</p>
{% for tags in post.tag %}
<a class="btn btn-primary" href="{{ path('get_posts_for_tags', {'tags': tags }) }}">{{ tags }} <span class="glyphicon glyphicon-chevron-right"></span></a>
{% endfor %}


public function indexAction(Request $request, $tag)
{
$em = $this->getDoctrine()->getManager();

$posts = $em->getRepository('PillsBundle:Post')
->getPostByTag($tags);

结束这个函数:

    public function getPostByTag($tag)
{
$date = new \DateTime;

$qb = $this->getEntityManager()->createQueryBuilder('p');

$qb
->select('p')
->from('PillsBundle:Post', 'p')
->getQuery();

$query = $qb->getQuery();
$results = $query->getResult();
// dump($results);exit; => I have all posts
// And I dont know what to do next, help
}

和类别:

    public function indexAction(Request $request, $category)
{
$em = $this->getDoctrine()->getManager();

$posts = $em->getRepository('PillsBundle:Post')
->getPostByCategory($category);
public function getPostByCategory($category)
{
$date = new \DateTime;

$qb = $this->getEntityManager()->createQueryBuilder('p');

$qb ->select('p')
->from('PillsBundle:Post', 'p')
->join('p.category', 'c')
->where('c = :category')
->setParameter('category', $category)
->getQuery();

$query = $qb->getQuery();
$results = $query->getResult();

return $results;
}

如果我使用这个 QueryBuilder,我需要为这个功能发布什么(现在我发布 categoty.slug)?请帮忙

最佳答案

您必须将标记传递到查询中,所以...

public function getPostByTag($tag)
{
$date = new \DateTime;

$qb = $this->getEntityManager()->createQueryBuilder('p');

$qb
->select('p')
->from('PillsBundle:Post', 'p')
->join('p.tag', 't')
->where('t = :tag')
->setParameter('tag', $tag)
->getQuery();

$query = $qb->getQuery();
$results = $query->getResult();

return $results;
}

应该是这样的,但是你应该改名字,很困惑,如果你只发送一个标签,你必须称它为标签,而不是标签。并且总是当你有很多属于一个实体的标签时,你必须称它为标签,复数形式

关于php - 如何查找实体 ManyToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502556/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com