2
  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();
}
kkochanski
  • 2,178
  • 23
  • 28
  • How is the Entity class? Do you have OneToMany() in the `comments`? Symfony [example](http://symfony.com/doc/current/book/doctrine.html#relationship-mapping-metadata) – tttony Aug 04 '14 at 20:26
  • I added some information about entities. I think that OneToMany() should be in News entity. – kkochanski Aug 04 '14 at 21:06
  • You shouldn't have to do join. Try just getting a list of elements, and accessing the comments like `{% for comment in article.comments %}`. Doctrine is aware of the relationship and is built to handle this sort of thing behind the scenes. – The Maniac Aug 04 '14 at 21:11
  • @TheManiac, something like this in controller? $repository = $this->getDoctrine()->getRepository('BlogAdminBundle:News'); $query = $repository->createQueryBuilder('p') ->where('p.date_active < :date') ->setParameter('date', new \DateTime()) ->getQuery(); $fetched_news = $query->getResult(); Then in twig this should work? {% for comment in fetched_news.comments %} {{ comment.content }} {% endfor %} {% for news in fetched_news.news %} {{ news.content }} {% endfor %} For me don't :( – kkochanski Aug 04 '14 at 21:24
  • I've added an answer, hopefully it helps! – The Maniac Aug 04 '14 at 21:37

1 Answers1

3

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.

The Maniac
  • 2,628
  • 3
  • 19
  • 29
  • This works!:D Can you also answer on second question? And if I want to count the number of comments I think that I need to join tables,yes? Something like this: `$query = $em->createQuery("SELECT news, COUNT(comments) FROM BlogAdminBundle:News news JOIN news.comments comments");` How can I get the number of this counting in twig? – kkochanski Aug 04 '14 at 22:05
  • Do you need the absolute total number of comments across all articles, or just the total number of comments for a single article? – The Maniac Aug 04 '14 at 22:07
  • `{{ news_article.comments|length }}` – The Maniac Aug 04 '14 at 22:16
  • Ah, I was so close :D Ok, you helped me a lot and I will mark your answer :) What do you do to see if you get proper data using DQL? I would like to do print_r($fetched_news) but it don't work properly. Guy there do it in the same way as I and he get better looking data. hmmm [stackoverflow question](http://stackoverflow.com/questions/14139439/symfony2-how-to-print-joined-tables-output) – kkochanski Aug 04 '14 at 22:36
  • The reason you're seeing a bunch off junk when you `print_r` is because Doctrine entity objects are relatively large and have recursion (i.e. an entity has children entities, its entities all the way down!). The best way to get "pure data" is to ask for an associative array: http://stackoverflow.com/questions/7259256/how-to-get-a-doctrine2-result-object-as-an-associative-array – The Maniac Aug 04 '14 at 22:45