Reputation: 299
New to Symfony2, have a question on what I'm doing wrong with my custom Doctrine2 query to find posts by slug.
I am using the following which works, but would like to change the sorting order to DESC.
$post = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->findOneBy(array(
'slug' => $slug
));
Here is my custom query:
public function findPostsBySlug($slug)
{
return $this->createQueryBuilder('post')
->select('post')
->where('post.slug = :slug')
->setParameter('slug', $slug)
->orderBy('post.createdAt', 'DESC')
->getQuery()
->getResult();
}
Am getting the following error:
Key "title" for array with keys "0" does not exist in AcmeDemoBundle:Post:show.html.twig at line 7
What am I doing wrong or what is missing in my custom query?
Twig
{% block body %}
{{ parent() }}
<div class="container">
<h2>{{ post.title }}</h2>
<p>
<small>Post by <em>{{ post.author }}</em> on <em>{{ post.createdAt|date }}</em></small>
</p>
<p>{{ post.body }}</p>
{% for reply in post.replies %}
<hr>
<p>
<small>Reply from <em>{{ reply.author }}</em> on {{ reply.createdAt|date }}</small>
</p>
<p>{{ reply.body }}</p>
{% endfor %}
<br>
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
<h4>Reply</h4>
{{ form(form, { action: path('acme_demo_post_createreply', { slug: post.slug }) }) }}
{% endif %}
</div>
{% endblock %}
Trying to sort DESC for replies to posts:
I am trying to sort my Replies to Post in DESC order. I tried to sort the posts in DESC order in hopes that the replies would also return in DESC order but it's not working and they're just returning in ASC order.
How can I sort replies to posts in DESC order?
Controller:
/**
* Show a post
*
* @param string $slug
*
* @throws NotFoundHttpException
* @return array
*
* @Route("/{slug}", name="acme_demo_post_show")
* @Template("AcmeDemoBundle:Post:show.html.twig")
*/
public function showAction($slug)
{
$post = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->findPostsBySlug($slug);
// Form for replies
$form = $this->createForm(new ReplyType());
return array(
'post' => $post,
'form' => $form->createView()
);
}
Query for posts by slug I am using:
public function findPostsBySlug($slug)
{
return $this->createQueryBuilder('post')
->select('post')
->where('post.slug = :slug')
->setParameter('slug', $slug)
->orderBy('post.createdAt', 'DESC')
->getQuery()
->getSingleResult();
}
Replies are mapped to Post as Many to Many:
/**
* @return Array Collection
*
* @ORM\ManyToMany(targetEntity="Reply", inversedBy="post")
* @JoinTable(name="posts_replies",
* joinColumns={@JoinColumn(name="post_id", referencedColumnName="id", nullable=true)},
* inverseJoinColumns={@JoinColumn(name="reply_id", referencedColumnName="id")}
* )
*/
protected $replies;
Twig to display replies to post:
{% for reply in post.replies %}
<hr>
<p>
<small>Reply from <em>{{ reply.author }}</em> on {{ reply.createdAt|date }}</small>
</p>
<p>{{ reply.body }}</p>
{% endfor %}
Upvotes: 2
Views: 1066
Reputation: 10513
The getResult()
function from Doctrine2 returns a collection of objects, which is similar to an array, that's why Twig can't display it. You can use a Twig loop to iterate over this collection of objects but that would be useless.
Your code should work by using getSingleResult()
instead of getResult()
.
About ordering comments, according to the documentation and this answer on SO you can add a @OrderBy
parameter:
/**
* @return Array Collection
*
* @ORM\ManyToMany(targetEntity="Reply", inversedBy="post")
* @ORM\OrderBy({"createdAt" = "DESC"})
* @JoinTable(name="posts_replies",
* joinColumns={@JoinColumn(name="post_id", referencedColumnName="id", nullable=true)},
* inverseJoinColumns={@JoinColumn(name="reply_id", referencedColumnName="id")}
* )
*/
protected $replies;
Upvotes: 2