可以对此使用一些指导 - 一些上下文,我在一个简单的博客中添加一个简单的分页代码,但是当你从分页中使用getArrayResult()时,它会错过一个字段('评论')我需要传递到我的twig文件中。
当使用getRepository(获取所有内容)时,它可以正常使用'注释'字段根据需要返回。评论是它自己的表与博客分开。有没有什么方法可以让我的设置分页的方式工作?
非常感谢任何帮助。
评论字段设置为“一对多”'对于我的博客实体。
博客实体
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="blog")
*/
protected $comments;
评论实体
/**
* @ORM\ManyToOne(targetEntity="Blog", inversedBy="comments")
* @ORM\JoinColumn(name="blog_id", referencedColumnName="id")
*/
protected $blog;
控制器getRepository
$em = $this->getDoctrine()->getManager();
$blogs = $em->getRepository('GeneralSymProjectBundle:Blog')
->getLatestBlogs();
分页代码
$page = $request->get('page');
$count_per_page = 5;
$total_count = $this->getTotalBlogs();
$total_pages = ceil($total_count/$count_per_page);
if (!is_numeric($page)) {
$page = 1;
} else {
$page = floor($page);
}
if ($total_count <= $count_per_page) {
$page = 1;
}
if (($page * $count_per_page) > $total_count) {
$page = $total_pages;
}
$offset = 0;
if ($page > 1) {
$offset = $count_per_page * ($page - 1);
}
$em = $this->getDoctrine()->getManager();
$blogQuery = $em->createQueryBuilder()
->select('b')
->from('GeneralSymProjectBundle:Blog', 'b')
->setFirstResult($offset)
->setMaxResults($count_per_page);
$blogFinalQuery = $blogQuery->getQuery();
$blogPage = $blogFinalQuery->getArrayResult();
存储库转储(返回注释的位置)
object(stdClass)[875]
public '__CLASS__' => string 'General\SymProjectBundle\Entity\Blog' (length=36)
public 'id' => int 2
public 'title' => string 'Another blog post' (length=17)
public 'author' => string 'KK' (length=2)
public 'blog' => string '...' (length=...)
public 'image' => string 'practice.jpg' (length=12)
public 'tags' => string 'symfony, php, blog, dummy text, fantasy, features, symproject' (length=61)
public 'comments' => string 'Array(2)' (length=8)
public 'created' => string 'DateTime' (length=8)
public 'updated' => string 'DateTime' (length=8)
public 'slug' => string 'another-blog-post' (length=17)
分页转储(缺少评论字段)
'id' => int 2
'title' => string 'Another blog post' (length=17)
'author' => string 'KK' (length=2)
'blog' => string '... (length=...)
'image' => string 'practice.jpg' (length=12)
'tags' => string 'symfony, php, blog, dummy text, fantasy, features, symproject' (length=61)
'created' => string 'DateTime' (length=8)
'updated' => string 'DateTime' (length=8)
'slug' => string 'another-blog-post' (length=17)
Index.html.twig
{# src/General/SymProjectBundle/Resources/views/Default/index.html.twig #}
{% extends 'GeneralSymProjectBundle::layout.html.twig' %}
{% block body %}
{% for blog in blogPage %}
<div class="container">
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">{{ blog.title }}</h2>
<p class="blog-post-meta"><time datetime="{{ blog.created|date('c') }}">{{ blog.created|date('l, F j, Y') }}</time> by <a href="#">{{ blog.author }}</a></p>
<div class="comment">
<p><small>Comments: {{ blog.comments|length }}</small></p>
</div>
<p>{{ blog.blog|truncate(350, true) }}</p><br>
<div class="tags">
<p><strong>Tags: </strong><span class="highlight">{{ blog.tags }}</span></p>
</div>
<p class="continue"><a href="{{ path('general_sym_project_show', { 'id': blog.id, 'slug': blog.slug }) }}">More reading» </a></p>
<hr>
</div><!-- /.blog-post -->
</div>
</div>
</div>
{% endfor %}
{% endblock %}
{% block sidebar %}
{{ parent() }}
{% endblock %}
答案 0 :(得分:1)
我猜测commments是一个单独的表,其中blog id作为外键。将此添加到分页代码中可能会获得您的博客帖子的评论。
foreach $blogPage as $blog{
$blog_id = $blog["id"] // U might have to modify these based on the result you get.
$commentRepository = $this->getDoctrine()
->getRepository('GeneralSymProjectBundle:Comment'');
$comments []= $repository->findByBlogId($blog_id);
}
将评论作为单独的变量传递,并尝试使用树枝:
{% block body %}
{% for blog in blogPage %}
.
.
<div class="comment">
<p><small>Comments: {{ comments[loop.index0]|length }}</small></p>
{% for comment in comments[loop.index0] %}
//display the comments here if you need
{% endfor %}
</div>
.
.
.
{% endfor %}
{% endblock %}
检查http://symfony.com/doc/current/book/doctrine.html以了解有关学说过滤的更多信息
答案 1 :(得分:0)
如果使用getArrayResult,则不会获得关系表的数据。
这一定对您有用。
$blogQuery = $em->createQueryBuilder()
->select('b, c')
->from('GeneralSymProjectBundle:Blog', 'b')
->leftJoin('b.Comments', 'c');