我创建了一个自定义实体" Faq"与他们的路线。 当我转储" faqs"在我的index.html.twig中,Faq#translations#collection是空的。
我需要一个"问题"以及一个"响应"在集合中。 在我的数据库中,所有字段都存在于FaqTranslation中。
我可能会错过什么?
抱歉我的英语不好。
谢谢。
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"
}
<?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();
}
}
<?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;
}
}
{% extends '@SyliusShop/layout.html.twig' %}
{% block content %}
{{ dump(faqs.translations.toArray)}}
{% endblock %}
答案 0 :(得分:0)
转储可以返回空集合,因为未初始化,请尝试使用{{ dump(faq.translations.toArray }}
。当使用时,PersistenCollections被初始化,例如,当得到至少一个元素时。