dqzlqfqeh845799833 2014-08-04 19:34 采纳率: 0%
浏览 40
已采纳

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

  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). 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.
  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

or

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

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();
}
  • 写回答

1条回答 默认 最新

  • dongxian4531 2014-08-04 21:36
    关注

    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 =).

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?