I have two entity Post and Tags this entity have relationship ManyToMany, many post have many tags and many tags have many posts and have Entity Category I Many post for one category and I need create action for find post for category and find post for tag
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;
category:
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;
post
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;
and my template I post in my action field 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);
end this is function:
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
}
and for category:
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;
}
If I use this QueryBuilder what I post (now I post categoty.slug) for this function needed? Help please
You have to pass the tag into the query so...
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;
}
Should be working like this, but you should change your names, is quite confusing, if you send just one tag, you have to call it tag, not tags. And always when you have many tags which belongs to an entity you have to call it tags, plural