0

I want to show information from one entity.

The entity that has the information is related to another , so I use a query to obtain that information.

class Playlist
{
    private $id;

    private $name;

    private $items;

    public function __construct()
    {
        $this->items = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function addItem(\Publicartel\AppBundle\Entity\PlaylistContent $content)
    {
        $content->setPlaylist($this);

        $this->items->add($content);

        return $this;
    }

    public function removeItem(\Publicartel\AppBundle\Entity\PlaylistContent $content)
    {
        $this->items->removeElement($content);
    }

    public function getItems()
    {
        return $this->items;
    }
}

class PlaylistContent
{
    private $content;

public function setContent(\Publicartel\AppBundle\Entity\Content $content = null)
    {
        $this->content = $content;

        return $this;
    }

    public function getContent()
    {
        return $this->content;
    }

}

// The controller:

$playlists = $em->getRepository('PublicartelAppBundle:Playlist')->getAllPlaylist();

return $this->render('PublicartelAppBundle:Player:calendar.html.twig', array( 
'playlists' => $playlists,
));

// The query

public function getAllPlaylist()
    {
        $em = $this->getEntityManager();

        $dql = 'SELECT p, cnt, plc FROM Publicartel\AppBundle\Entity\Playlist p
                LEFT JOIN p.items cnt
                LEFT JOIN cnt.content plc';

        $query = $this->getEntityManager()
            ->createQuery($dql)
            ->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);

        return $query->execute();
    }

The consultation seeks elements of the content entity, so it takes a left join on ' items' and 'content'.

// The twig template

I have sought access to the element of two ways:

{% for playlist in playlists.items.content %}
    <img src="/{{ playlist.path}}"> 
{% endfor %}

Key "items" for array with keys " 0, 1 " does not exist in PublicartelAppBundle : Player : calendar.html.twig at line 215

{% for playlist in playlists %}
    <img src="/{{ playlist.items.content.path }}">
{% endfor %}

Key "content" for array with keys "0, 1" does not exist in PublicartelAppBundle:Player:calendar.html.twig at line 223

'Path' is an attribute for entity 'Content' that I want show.

2 Answers 2

1

I guess you need to loop like this:

{% for playlistContent in playlists.items %}
    {% if playlistContent.content is not null %}
        <img src="/{{ playlistContent.content.path }}"> 
    {% endif %}
{% endfor %}

EDIT:

That found:

{% for playlist in playlists %}
    {% for playlistContent in playlist.items %}
        <img src="/{{ playlistContent.content.screenshot}}">
        <img src="/{{ playlistContent.content.path}}">
    {% endfor %}    
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

0

playlists.items is an array of objects not an object. Therefore you must loop through it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.