简体   繁体   English

Symfony 2-如何在Twig模板中显示已连接的教义实体

[英]Symfony 2 - How to display joined Doctrine entities in Twig template

  1. How to retrieve entity data in twig? 如何检索树枝中的实体数据? I selected two entities, News and Comments, but in twig I can only get data from News entity. 我选择了两个实体,即新闻和评论,但是在树枝中,我只能从新闻实体中获取数据。 (example: "From Twig") (例如:“来自树枝”)
  2. How to see what is in the fetched entity object( for example: $fetched_news variable from Controller). 如何查看所获取的实体对象中的内容(例如:Controller中的$ fetched_news变量)。 I have tried: print_r($fetched_news) or {{ dump(fetched_news }}, but in first example I get the full screen of code and in second application get suspended. 我已经尝试过:print_r($ fetched_news)或{{dump(fetched_news}}},但是在第一个示例中,我得到了全屏代码,而在第二个应用程序中,则被暂停了。
  3. In News and Comments entities, there is the same column named 'content'. 在新闻和评论实体中,有一个名为“内容”的列。 How the get the data from this columns? 如何从此列获取数据? I was trying something like this: 我正在尝试这样的事情:

fetched_news.comments.content fetched_news.comments.content

or 要么

fetched_news.news.content fetched_news.news.content

I was looking for the answer on many pages, but I couldn't find something interesting. 我在很多页面上都在寻找答案,但是找不到任何有趣的东西。

From twig: 从树枝:

{% 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 %}

From Controller: 从控制器:

    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);
}

Code from Web Profiler 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

Thanks for help! 感谢帮助!

Entity class Comments: 实体类评论:

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

Entity class News: 实体类新闻:

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

Given the following code: 给出以下代码:

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

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

$fetched_news = $query->getResult(); 

This should work in twig: 这应该在树枝上工作:

{% for news_article in fetched_news %} 

    {{ news_article.content }}

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

{% endfor %}

Each of your news articles has an array of comments. 您的每篇新闻文章都有一系列评论。 I think you're just getting a little mixed up with your $fetched_news variable =). 我认为您只是将$fetched_news变量=) $fetched_news

Edit: I had a small mistake in my code. 编辑:我在代码中有一个小错误。 I had fetched_news.news in the outer loop, and that should be just feteched_news since that variable is the array of news articles. 我在外循环中有fetched_news.news ,那应该只是feteched_news因为该变量是新闻文章的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM