【问题标题】:Symfony 2 - How to display joined Doctrine entities in Twig templateSymfony 2 - 如何在 Twig 模板中显示加入的 Doctrine 实体
【发布时间】:2014-08-04 19:34:58
【问题描述】:
  1. 如何在树枝中检索实体数据?我选择了两个实体,新闻和评论,但在树枝中我只能从新闻实体获取数据。 (例如:“来自树枝”)
  2. 如何查看获取的实体对象中的内容(例如:来自 Controller 的 $fetched_news 变量)。我尝试过:print_r($fetched_news) 或 {{ dump(fetched_news }},但在第一个示例中,我获得了全屏代码,而在第二个应用程序中被暂停。
  3. 在新闻和评论实体中,有一个名为“内容”的相同列。如何从这些列中获取数据?我正在尝试这样的事情:

fetched_news.cmets.content

fetched_news.news.content

我在很多页面上都在寻找答案,但找不到有趣的东西。

来自树枝:

{% for news in fetched_news %}
   <div class="col-md-5">
        <p class="news_title">{{ news.title }}</p>
        {{ (news.content|slice(0,600))|raw }}

         {{ news.ratePlus }} {# CAN'T GET THIS!#}
    {% else %}
{% endfor %}

来自控制器:

    public function indexAction()
{
    $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery("SELECT n, a FROM BlogAdminBundle:News n JOIN n.comments a");
    $fetched_news = $query->getResult();


    return array('fetched_news' => $fetched_news);
}

来自 Web Profiler 的代码

SELECT 
n0_.id AS id0, 
n0_.content AS content1, 
n0_.title AS title2, 
n0_.date_add AS date_add3, 
n0_.date_active AS date_active4, 
n0_.settings AS settings5, 
c1_.id AS id6, 
c1_.content AS content7, 
c1_.date_add AS date_add8, 
c1_.rate_plus AS rate_plus9, 
c1_.rate_minus AS rate_minus10, 
n0_.user_id AS user_id11, 
c1_.user_id AS user_id12, 
c1_.news_id AS news_id13 
FROM 
News n0_ 
INNER JOIN Comments c1_ ON n0_.id = c1_.news_id

感谢您的帮助!

实体类注释:

 /**
 * @ORM\ManyToOne(targetEntity="News", inversedBy="comments")
 * @ORM\JoinColumn(name="news_id", referencedColumnName="id")
 */
protected $news;

实体类新闻:

 /**
 * @ORM\OneToMany(targetEntity="Comments", mappedBy="news")
 */
protected $comments;
public function __construct()
{
    $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
}

【问题讨论】:

  • Entity类怎么样? comments 中有 OneToMany() 吗? Symfony example
  • 我添加了一些关于实体的信息。我认为 OneToMany() 应该在 News 实体中。
  • 你不应该加入。尝试获取元素列表,然后访问像 {% for comment in article.comments %} 这样的 cmets。 Doctrine 意识到了这种关系,并且旨在在幕后处理这类事情。
  • @TheManiac,控制器中有这样的东西吗? $repository = $this->getDoctrine()->getRepository('BlogAdminBundle:News'); $query = $repository->createQueryBuilder('p') ->where('p.date_active setParameter('date', new \DateTime()) ->getQuery(); $fetched_news = $query->getResult();那么在树枝中这应该有效吗? {% for comment in fetched_news.cmets %} {{ comment.content }} {% endfor %} {% for news in fetched_news.news %} {{ news.content }} {% endfor %} 对我来说不要: (
  • 我已经添加了答案,希望对您有所帮助!

标签: php symfony doctrine-orm twig


【解决方案1】:

给定以下代码:

$repository = $this->getDoctrine()->getRepository('BlogAdminBundle:News');

$query = $repository->createQueryBuilder('p') 
    ->where('p.date_active < :date') 
    ->setParameter('date', new \DateTime()) 
    ->getQuery(); 

$fetched_news = $query->getResult(); 

这应该在树枝中工作:

{% for news_article in fetched_news %} 

    {{ news_article.content }}

    {% for comment in news_article.comments %}
        {{ comment.content }}
    {% endfor %}

{% endfor %}

您的每篇新闻文章都有一个 cmets 数组。我认为你只是有点混淆了$fetched_news 变量 =)。

编辑:我的代码中有一个小错误。我在外循环中有fetched_news.news,应该只是feteched_news,因为该变量是新闻文章数组。

【讨论】:

  • 这行得通!:D 你能回答第二个问题吗?如果我想计算我认为我需要加入表格的 cmets 的数量,是吗?像这样的东西:$query = $em-&gt;createQuery("SELECT news, COUNT(comments) FROM BlogAdminBundle:News news JOIN news.comments comments");我怎样才能得到树枝中这个计数的数量?
  • 您需要的是所有文章的绝对总 cmets 数,还是单篇文章的 cmets 总数?
  • {{ news_article.comments|length }}
  • 啊,我是如此接近 :D 好的,你帮了我很多,我会标记你的答案 :) 你如何查看是否使用 DQL 获得了正确的数据?我想做 print_r($fetched_news) 但它不能正常工作。那里的人以与我相同的方式执行此操作,并且他获得了更好看的数据。嗯stackoverflow question
  • print_r 看到一堆垃圾的原因是 Doctrine 实体对象相对较大并且具有递归(即一个实体有子实体,它的实体一直向下!) .获取“纯数据”的最佳方式是请求关联数组:stackoverflow.com/questions/7259256/…
猜你喜欢
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多