有人可以解释一下我做错了什么。我有一个实体,其类包含Actualite和ActualiteTranslation。
我用sonanta创建了ActualiteAdmin类:admin:generate。
一切都按预期工作。我在控制器中使用dump($ realite)调试字段,我看到每个字段都显示良好:
array:1 [▼
0 => Actualite {#742 ▼
-id: 8
-date: DateTime {#806 ▶}
-image: null
#translations: PersistentCollection {#709 ▼
-snapshot: array:1 [ …1]
-owner: Actualite {#742}
-association: array:16 [ …16]
-em: EntityManager {#789 …11}
-backRefFieldName: "translatable"
-typeClass: ClassMetadata {#738 …}
-isDirty: false
#collection: ArrayCollection {#662 ▼
-elements: array:1 [▼
"fr" => ActualiteTranslation {#705 ▼
#actucategorie: "categorie"
#alt: "image"
#titre: "Page Facebook"
#article: "mon article"
#id: 4
#locale: "fr"
#translatable: Actualite {#742}
#slug: "page-facebook"
}
]
}
#initialized: true
}
#newTranslations: null
#currentLocale: "fr"
#defaultLocale: "fr"
}
]
使用{{actualite.translations.titre}}时,我在Twig模板中出现错误:
键“0”的数组的键“翻译”不存在。
这是我使用的代码:
namespace BDN\ActualitesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* Actualite
*
* @ORM\Table(name="bdn_actualite")
* @ORM\Entity(repositoryClass="BDN\ActualitesBundle\Repository\ActualiteRepository")
*
*/
class Actualite
{
use ORMBehaviors\Translatable\Translatable;
/**
* @param $method
* @param $args
*
* @return mixed
*/
public function __call($method, $args)
{
if (!method_exists(self::getTranslationEntityClass(), $method)) {
$method = 'get' . ucfirst($method);
}
return $this->proxyCurrentLocaleTranslation($method, $args);
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255, nullable=true)
*/
private $image;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set date
*
* @param \DateTime $date
*
* @return null
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set image
*
* @param string $image
*
* @return null
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
}
namespace BDN\ActualitesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* ActualiteTranslation
*
* @ORM\Entity
* @ORM\Table(name="bdn_actualite_translation")
*
*/
class ActualiteTranslation
{
use ORMBehaviors\Translatable\Translation;
use ORMBehaviors\Sluggable\Sluggable;
/**
* @var string
*
* @ORM\Column(name="actucategorie", type="string", length=50)
*/
protected $actucategorie;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
protected $alt;
/**
* @var string
*
* @ORM\Column(name="titre", type="string", length=255, unique=true)
*/
protected $titre;
public function getSluggableFields()
{
return ['titre'];
}
/**
* @var string
*
* @ORM\Column(name="article", type="text")
*/
protected $article;
/**
* Set actucategorie
*
* @param string $actucategorie
*
* @return ActualiteTranslation
*/
public function setActucategorie($actucategorie)
{
$this->actucategorie = $actucategorie;
return $this;
}
/**
* Get actucategorie
*
* @return string
*/
public function getActucategorie()
{
return $this->actucategorie;
}
/**
* Set alt
*
* @param string $alt
*
* @return ActualiteTranslation
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
/**
* Set titre
*
* @param string $titre
*
* @return ActualiteTranslation
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Set article
*
* @param string $article
*
* @return ActualiteTranslation
*/
public function setArticle($article)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* @return string
*/
public function getArticle()
{
return $this->article;
}
}
<!-- begin snippet: js hide: false console: true babel: false -->
<?php
namespace BDN\ActualitesBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
class ActualiteAdmin extends AbstractAdmin
{
/**
* @param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('id')
->add('date')
->add('image')
;
}
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id')
->add('date')
->add('image')
->add('_action', null, array(
'actions' => array(
'show' => array(),
'edit' => array(),
'delete' => array(),
),
))
;
}
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('date')
->add('image')
->add("translations", TranslationsType::class, array(
"fields" => array(
"actucategorie" => [],
"alt" => [],
"titre" => [],
"slug" => ["display" => false,],
"article" => [ "field_type" => "text"],
),
))
;
}
/**
* @param ShowMapper $showMapper
*/
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('id')
->add('date')
->add('image')
->add("translations", TranslationsType::class, array(
"required_locales" => ['fr'],
"fields" => array(
"actucategorie" => [],
"alt" => [],
"titre" => [],
"article" => [ "field_type" => "text"],
),
))
;
}
}
{% extends "::base.html.twig" %}
{% block page %}
<section class="row jumbotron">
<div class="col-lg-12">
<div class="row">
<div class="col-xs-9">
<h3>{{ actualite.getTitre() }}</h3>
<p class="categorie">Le {{ actualite.date | date('d/m/Y') }} | Catégorie : {{ actualite.translate.actucategorie }}</p>
</div>
{% if actualite.image is not null %}
<div class="col-xs-3 hidden-xs">
<img src="{{ asset('actualite.image') }}" class="img-thumbnail img-shop" alt="{{ actualite.alt }}">
</div>
{% endif %}
</div>
<div class="row content-actualite">
<div class="col-lg-12">
{{ actualite.translate.article }}
</div>
</div>
</div>
</section>
{% endblock %}
非常感谢任何帮助!
丹尼斯