Symfony2の初心者で、スラッグで投稿を見つけるためのカスタムDoctrine2クエリで何が間違っているのか質問があります。
以下を使用していますが、ソート順をDESCに変更したいと思います。
$post = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->findOneBy(array(
'slug' => $slug
));
これが私のカスタムクエリです:
public function findPostsBySlug($slug)
{
return $this->createQueryBuilder('post')
->select('post')
->where('post.slug = :slug')
->setParameter('slug', $slug)
->orderBy('post.createdAt', 'DESC')
->getQuery()
->getResult();
}
次のエラーが発生しています:
Key "title" for array with keys "0" does not exist in AcmeDemoBundle:Post:show.html.twig at line 7
何が間違っているのですか、またはカスタムクエリに何が欠けていますか?
小枝
{% 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 %}
投稿への返信用にDESCを並べ替えようとしています:
投稿への返信をDESC順に並べ替えようとしています。返信もDESCの順序で返されることを期待して、投稿をDESCの順序で並べ替えようとしましたが、機能せず、ASCの順序で返されるだけです。
投稿への返信をDESC順に並べ替えるにはどうすればよいですか?
コントローラ:
/**
* 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()
);
}
私が使用しているスラッグによる投稿のクエリ:
public function findPostsBySlug($slug)
{
return $this->createQueryBuilder('post')
->select('post')
->where('post.slug = :slug')
->setParameter('slug', $slug)
->orderBy('post.createdAt', 'DESC')
->getQuery()
->getSingleResult();
}
返信は多対多の投稿にマッピングされます。
/**
* @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;
投稿への返信を表示する小枝:
{% 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 %}
getResult()
Doctrine2の関数は、配列に似たオブジェクトのコレクションを返します。そのため、Twigはそれを表示できません。Twigループを使用して、このオブジェクトのコレクションを反復処理できますが、それは役に立ちません。
コードは、のgetSingleResult()
代わりにを使用して機能するはずですgetResult()
。
コメントの注文については、ドキュメントとSOに関するこの回答に従って、@OrderBy
パラメータを追加できます。
/**
* @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;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加