gpt4 book ai didi

Symfony2 - 设置相关类别以动态发布

转载 作者:行者123 更新时间:2023-12-02 16:09:58 25 4
gpt4 key购买 nike

我是 Sf2/Doctrine2 的新手。我有一个学说查询,它检索同一类别中的相关帖子并排除当前显示的帖子。

我需要弄清楚如何动态传递类别,这样我就不必在 Controller 中设置它。

显示路线显示来自不同类别的帖子,我只想显示与该特定类别相关的帖子,而不在 Controller 中对其进行硬编码。

我可以获得有关如何修复此学说查询的帮助吗?

学说查询

public function getRelatedPosts($exceptPost, $limit, $category) // Using Doctrine to exclude the shown post
{
return $this
->createQueryBuilder('post')
->leftJoin('post.category','category')
->where('post != :exceptPost')
->andWhere('category.title = :category')
->setParameter('category', $category)
->setMaxResults($limit)
->orderBy('post.createdAt', 'DESC')
->setParameter('exceptPost', $exceptPost)
->getQuery()
->execute();
}

Controller (在 Controller 中设置类别——如何动态设置?)

/**
* Show action for post
*
* @param string $slug
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @return array
*
* @Route("/{catslug}/{slug}", name="acme_demo_page_show")
* @Template("AcmeDemoBundle:Page:show.html.twig")
*/
public function showAction($slug)
{
$post = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->findOneBy(array(
'slug' => $slug
));

if (null === $post) {
throw $this->createNotFoundException('Post was not found');
}

$posts = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->getRelatedPosts($post, 4, 'articles');

return array(
'post' => $post,
'posts' => $posts
);
}

类别实体

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

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

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

/**
* @var string
*
* @Gedmo\Slug(fields={"title"}, unique=false)
* @ORM\Column(length=255)
*/
private $catslug;


public function __construct()
{
$this->posts = new ArrayCollection();
}

public function __toString()
{
return $this->getTitle() ? $this->getTitle() : "";
}

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

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

return $this;
}

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

/**
* Add posts
*
* @param \Acme\DemoBundle\Entity\Post $posts
* @return Category
*/
public function addPost(\Acme\DemoBundle\Entity\Post $posts)
{
$this->posts[] = $posts;

return $this;
}

/**
* Remove posts
*
* @param \Acme\DemoBundle\Entity\Post $posts
*/
public function removePost(\Acme\DemoBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
}

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

/**
* Set catslug
*
* @param string $catslug
* @return Category
*/
public function setCatSlug($catslug)
{
$this->slug = $catslug;

return $this;
}

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

发布实体

/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;

/**
* Set category
*
* @param \Acme\AcmeDemoBundle\Entity\Category $category
* @return Post
*/
public function setCategory(\Acme\AcmeDemoBundle\Entity\Category $category = null)
{
$this->category = $category;

return $this;
}

/**
* Get category
*
* @return \Acme\AcmeDemoBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}

最佳答案

从您的 Controller 中使用 $post->getCategory() 检索帖子类别。在您的查询中,您所要做的就是 ->setParameter('category', $category)

关于Symfony2 - 设置相关类别以动态发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24858055/

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