0

I created a custom entity "Faq" with their routes . When I make a dump of "faqs" in my index.html.twig Faq#translations#collection is empty.

I need a "question"and a "response" in the collection. In my database all fields are present in FaqTranslation.

What I've could missed?

Sorry for my poor English.

Thanks .

result dump(faqs)

Faq {#3359 ▼
-id: 2
#translations: PersistentCollection {#3369 ▼
-snapshot: []
-owner: Faq {#3359}
-association: array:16 [ …16]
-em: EntityManager {#2123 …11}
-backRefFieldName: "translatable"
-typeClass: ClassMetadata {#3361 …}
-isDirty: false
#collection: ArrayCollection {#3371 ▼
-elements: []
}
#initialized: false
}
#translationsCache: []
#currentLocale: "fr_FR"
#currentTranslation: null
#fallbackLocale: "en_US"
}

Faq Entity

<?php
namespace AppBundle\Entity;



use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;
use Sylius\Component\Resource\Model\TranslatableTrait;

/**
 * Faq
 */
class Faq implements ResourceInterface, TranslatableInterface
{
    use TranslatableTrait {
        __construct as private initializeTranslationsCollection;
    }

    public function __construct()
    {
        $this->initializeTranslationsCollection();
    }
    /**
     * @var int
     */
    private $id;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

        /**
     * Set response
     *
     * @param string $response
     *
     * @return FaqTranslation
     */
    public function setResponse($response)
    {
        return $this->getTranslation()->setResponse($response);

    }

    /**
     * Get response
     *
     * @return string
     */
    public function getResponse()
    {
        return $this->getTranslation()->getResponse();
    }

    /**
     * Set question
     *
     * @param string $question
     *
     * @return FaqTranslation
     */
    public function setQuestion($question)
    {
        return $this->getTranslation()->setQuestion($question);

    }

    /**
     * Get question
     *
     * @return string
     */
    public function getQuestion()
    {
        return $this->getTranslation()->getQuestion();
    }
    /**
     * Set locale
     *
     * @param string $locale
     *
     * @return FaqTranslation
     */
    public function setLocale($locale)
    {
        return $this->getTranslation()->setLocale($locale);
    }

    /**
     * Get locale
     *
     * @return string $locale
     */
    public function getLocale()
    {
        return $this->getTranslation()->getLocale();
    }
    /**
     * {@inheritdoc}
     */
    protected function createTranslation()
    {
        return new FaqTranslation();
    }


}

FaqTranslation Entity

<?php

namespace AppBundle\Entity;

use Sylius\Component\Resource\Model\AbstractTranslation;
use Sylius\Component\Resource\Model\ResourceInterface;

/**
 * FaqTranslation
 */
class FaqTranslation extends AbstractTranslation implements ResourceInterface
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $response;

    /**
     * @var string
     */
    private $question;

    protected $locale;
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set response
     *
     * @param string $response
     *
     * @return FaqTranslation
     */
    public function setResponse($response)
    {
        $this->response = $response;

        return $this;
    }

    /**
     * Get response
     *
     * @return string
     */
    public function getResponse()
    {
        return $this->response;
    }

    /**
     * Set question
     *
     * @param string $question
     *
     * @return FaqTranslation
     */
    public function setQuestion($question)
    {
        $this->question = $question;

        return $this;
    }

    /**
     * Get question
     *
     * @return string
     */
    public function getQuestion()
    {
        return $this->question;
    }

    /**
     * Set locale
     *
     * @param string $locale
     *
     * @return FaqTranslation
     */
    public function setLocale($locale)
    {
        $this->locale = $locale;

        return $this;
    }

    /**
     * Get locale
     *
     * @return string $locale
     */
    public function getLocale()
    {
        return $this->locale;
    }
}

views/Faq/index.html.twig

{% extends '@SyliusShop/layout.html.twig' %}

{% block content %}
{{ dump(faqs.translations.toArray)}}
{% endblock %}

1 Answers1

0

The dump can return a empty collection because is not initialized, try with {{ dump(faq.translations.toArray }}. PersistenCollections are initalized when are used, e.g. when get at least one element.

rafrsr
  • 1,940
  • 3
  • 15
  • 31
  • Thanks @rafrsr I tried and I get this error . `Neither the property "translations" nor one of the methods "translations()", "gettranslations()"/"istranslations()" or "__call()" exist and have public access in class "Pagerfanta\Pagerfanta".` – Gaetan Puleo May 08 '17 at 21:17
  • **faq** should be instance of Faq entity and should have a public method to get translations, can edit your question to share your entity and twig source code. – rafrsr May 08 '17 at 21:26
  • Ensure the object you are dumping is instance of Faq and have the method getTranslations(), you are missing something that I can`t see in the above code. Can share your controller to view the variables passed to twig. – rafrsr May 08 '17 at 23:29