symfony - Symfony2 - Doctrine query for finding posts by slug has an error -
new symfony2, have question on i'm doing wrong custom doctrine2 query find posts slug.
i using following works, change sorting order desc.
$post = $this->getdoctrine()->getrepository('acmedemobundle:post') ->findoneby(array( 'slug' => $slug ));
here 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 following error:
key "title" array keys "0" not exist in acmedemobundle:post:show.html.twig @ line 7
what doing wrong or missing in custom query?
twig
{% block body %} {{ parent() }} <div class="container"> <h2>{{ post.title }}</h2> <p> <small>post <em>{{ post.author }}</em> on <em>{{ post.createdat|date }}</em></small> </p> <p>{{ post.body }}</p> {% reply in post.replies %} <hr> <p> <small>reply <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 sort desc replies posts:
i trying sort replies post in desc order. tried sort posts in desc order in hopes replies return in desc order it's not working , they're returning in asc order.
how can sort replies posts in desc order?
controller:
/** * show 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 replies $form = $this->createform(new replytype()); return array( 'post' => $post, 'form' => $form->createview() ); }
query posts slug 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 mapped post many 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 display replies post:
{% reply in post.replies %} <hr> <p> <small>reply <em>{{ reply.author }}</em> on {{ reply.createdat|date }}</small> </p> <p>{{ reply.body }}</p> {% endfor %}
the getresult()
function doctrine2 returns collection of objects, similar array, that's why twig can't display it. can use twig loop iterate on collection of objects useless.
your code should work using getsingleresult()
instead of getresult()
.
about ordering comments, according documentation , this answer on so can add @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;
Comments
Post a Comment