urreta17
urreta17

Reputation: 183

Symfony displaying object value in twig

I have an entity Playlist that is related to another call Items.

I want to deploy on a twig template id of each of the items.

class Playlist
{
    private $items;

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

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

        $this->duration += $content->getDuration();

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

        return $this;
    }

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

        $this->duration -= $content->getDuration();
    }

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

I want to deploy in the Playlist form the id of the item.

I've tried like so:

{% for content in edit_form.items %}
{{ content.getId() }}
{{ content.id() }}
{% endfor %}

But I get the following error:

Method "getId" for object "Symfony\Component\Form\FormView" does not exist Method "id" for object "Symfony\Component\Form\FormView" does not exist

I've added the id attribute to my FormType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('id')
        ->add('position')
        ->add('duration')
        ->add('playlist')
        ->add('content')
    ;
}

But I get the following error:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Symfony\Component\Form\FormView could not be converted to string")

I've also tried:

// Controller

$entity = $em->getRepository('PublicartelAppBundle:Playlist')->find($id);

return $this->render('PublicartelAppBundle:Playlist:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));

// Twig

{{ entity.items.id }}

But that too throws an error:

Method "id" for object "Doctrine\ORM\PersistentCollection" does not exist

Thanks!

Upvotes: 1

Views: 7138

Answers (1)

DonCallisto
DonCallisto

Reputation: 29912

Solution 1

Last part of your solution is the good one

$entity = $em->getRepository('PublicartelAppBundle:Playlist')->find($id);

return $this->render('PublicartelAppBundle:Playlist:edit.html.twig', array(
            'entity'      => $entity,
            [...]
        ));

What isn't good is that snippet of code

{{ entity.items.id }}

You have to cycle over all items before print id out (from your error, is pretty clear)

{% for item in entity.items %}
  {{ item.id }}
{% endfor %}

Solution 2 (Didn't tested)

You can, of course, access also data from underlying object of a form, without pass it from the controller. So you can change your snippet of code from

from

{% for content in edit_form.items %}
  {{ content.getId() }}
  {{ content.id() }}
{% endfor %}

to

{% for content in edit_form.vars.data %}
  {{ content.id() }}
{% endfor %}

Your error was that you were trying to access FormView (passed from controller) and not entity "binded" to the form

Upvotes: 0

Related Questions