使用symfony2

时间:2016-07-06 10:12:20

标签: symfony doctrine a2lix-translation

我在github上阅读了所有文档和示例,但我无法弄清楚如何使doctrine-translatable-bundle + A2LiX Translation Form与我的项目一起工作。

这是我的 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;

我的 Article.php 实体

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Prezent\Doctrine\Translatable\Annotation as 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;
    }


}

我的 ArticleTranslation.php 实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Prezent\Doctrine\Translatable\Annotation as 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 and currentLocale is 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;
    }

}

最后是 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(),
                        )
        );
    }

}

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"

当然,两者都在 AppKernel.php

中注册并加载

当我提交文章时,我收到了这个错误:

 Notice: Undefined property: AppBundle\Entity\ArticleTranslation::$currentLocale
500 Internal Server Error - ContextErrorException 

堆栈跟踪

    in src\AppBundle\Entity\ArticleTranslation.php at line 34  -
            public function translate($locale = null) {
                if (null === $locale) {
                    $locale = $this->currentLocale; // here is the error
                }
                if (!$locale) { 

我缺少什么?

P.S。由于服务器上的PHP 5.3.29,我无法使用KnpLabs / DoctrineBehaviors。 : - (

0 个答案:

没有答案