symfony - Can't manage to work doctrine-translatable-bundle with symfony2 -
i read documentation , examples @ github, can't figure how make doctrine-translatable-bundle + a2lix translation form work project.
this autoload.php
<?php use doctrine\common\annotations\annotationregistry; use composer\autoload\classloader; /** * @var classloader $loader */ $loader = require __dir__.'/../vendor/autoload.php'; $loader->add('prezent\\doctrine\\translatable', __dir__ . '/../lib'); annotationregistry::registerloader(array($loader, 'loadclass')); annotationregistry::registerautoloadnamespace('prezent\\doctrine\\translatable\\annotation', __dir__ . '/../lib'); return $loader;
my article.php entity
<?php namespace appbundle\entity; use doctrine\common\collections\arraycollection; use doctrine\orm\mapping orm; use prezent\doctrine\translatable\annotation prezent; use prezent\doctrine\translatable\entity\abstracttranslatable; /** * @orm\entity * @orm\table(name="articles") */ class article extends abstracttranslatable { /** * @orm\id * @orm\generatedvalue(strategy="auto") * @orm\column(type="integer") */ protected $id; /** * @prezent\translations(targetentity="appbundle\entity\articletranslation") */ protected $translations; /** * @prezent\currentlocale */ private $currentlocale; public function __construct() { $this->translations = new arraycollection(); } function getid() { return $this->id; } function gettranslations() { return $this->translations; } function getcurrentlocale() { return $this->currentlocale; } function setid($id) { $this->id = $id; } function settranslations($translations) { $this->translations = $translations; } function setcurrentlocale($currentlocale) { $this->currentlocale = $currentlocale; } }
my articletranslation.php entity
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use prezent\doctrine\translatable\annotation prezent; use prezent\doctrine\translatable\entity\abstracttranslation; /** * @orm\entity * @orm\table(name="articles_translation") */ class articletranslation extends abstracttranslation { /** * @prezent\translatable(targetentity="appbundle\entity\article") */ protected $translatable; /** * @orm\column(type="string") */ private $title = ""; /** * @orm\column(type="text") */ private $content = ""; private $currenttranslation; public function translate($locale = null) { if (null === $locale) { $locale = $this->currentlocale; } if (!$locale) { throw new \runtimeexception('no locale has been set , currentlocale empty'); } if ($this->currenttranslation && $this->currenttranslation->getlocale() === $locale) { return $this->currenttranslation; } if (!$translation = $this->translations->get($locale)) { $translation = new articletranslation(); $translation->setlocale($locale); $this->addtranslation($translation); } $this->currenttranslation = $translation; return $translation; } public function gettitle() { return $this->translate()->gettitle(); } public function settitle($title) { $this->translate()->settitle($title); return $this; } public function getcontent() { return $this->translate()->getcontent(); } public function setcontent($content) { $this->translate()->setcontent($content); return $this; } }
and articlescontroller.php
<?php namespace appbundle\controller; use sensio\bundle\frameworkextrabundle\configuration\route; use symfony\bundle\frameworkbundle\controller\controller; use symfony\component\httpfoundation\request; use appbundle\entity\article; use metadata\metadatafactory; use prezent\doctrine\translatable\mapping\driver\doctrineadapter; use prezent\doctrine\translatable\eventlistener\translatablelistener; class articlescontroller extends controller { /** * @route("/admin/articles/new", name="articles_new") */ public function newaction(request $request) { if (!$this->get('security.authorization_checker')->isgranted('is_authenticated_fully')) { throw $this->createaccessdeniedexception(); } $em = $this->getdoctrine()->getmanager(); $driver = doctrineadapter::frommanager($em); $metadatafactory = new metadatafactory($driver); $translatablelistener = new translatablelistener($metadatafactory); $em->geteventmanager()->addeventsubscriber($translatablelistener); $translatablelistener->setcurrentlocale('en'); $article = new article(); $form = $this->createformbuilder($article) ->add('translations', 'a2lix_translations') ->add('save', "symfony\component\form\extension\core\type\submittype", array( 'label' => 'newarticle.save') ) ->getform(); $form->handlerequest($request); if ($form->issubmitted() && $form->isvalid()) { $em->persist($article); $em->flush(); $request->getsession()->getflashbag()->add('notice', 'newarticle.saved'); return $this->redirecttoroute('articles_new'); } return $this->render('appbundle:adminarticles:new.html.twig', array( 'form' => $form->createview(), ) ); } }
part of config.yml
prezent_doctrine_translatable: fallback_locale: en a2lix_translation_form: locale_provider: default locales: [en, bg, ru] default_locale: en required_locales: [en] manager_registry: doctrine templating: "a2lixtranslationformbundle::default.html.twig"
sure, both registered , loaded in appkernel.php
when submit article, got error:
notice: undefined property: appbundle\entity\articletranslation::$currentlocale 500 internal server error - contexterrorexception
stack trace
in src\appbundle\entity\articletranslation.php @ line 34 - public function translate($locale = null) { if (null === $locale) { $locale = $this->currentlocale; // here error } if (!$locale) {
what i'm missing?
p.s. can't use knplabs/doctrinebehaviors because of php 5.3.29 on server. :-(
Comments
Post a Comment