ZF2和Doctrine 2:翻译实体形式

时间:2016-11-07 14:01:06

标签: php doctrine-orm zend-framework2 zend-form doctrine-extensions

我需要为Doctrine可翻译实体创建ZF2表单(我使用https://github.com/Atlantic18/DoctrineExtensions可翻译扩展),它应该为每种可用语言中的实体的所有可翻译属性(列)提供字段。 到目前为止,我有以下内容:

1)文章实体

namespace TestModule\Entity;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Doctrine\ORM\Mapping\Entity(repositoryClass="TestModule\Entity\ArticleRepository")
 * @Doctrine\ORM\Mapping\Table(name="test_module_articles")
 * @Gedmo\Mapping\Annotation\TranslationEntity(class="TestModule\Entity\ArticleTranslation")
 */
class Article
{
    /**
     * @var int Auto-Incremented Primary Key
     *
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\Column(type="integer")
     * @Doctrine\ORM\Mapping\GeneratedValue
     */
    protected $id;

    /**
     * @Doctrine\ORM\Mapping\Column(type="string")
     * @Gedmo\Mapping\Annotation\Translatable
     */
    protected $name;

    /**
     * @Doctrine\ORM\Mapping\Column(type="text", length=65535)
     * @Gedmo\Mapping\Annotation\Translatable
     */
    protected $description;

    /**
     * @Gedmo\Mapping\Annotation\Locale
     * Used locale to override Translation listener`s locale
     * this is not a mapped field of entity metadata, just a simple property
     * and it is not necessary because globally locale can be set in listener
     */
    private $locale;

    /**
     * @Doctrine\ORM\Mapping\OneToMany(
     *   targetEntity="TestModule\Entity\ArticleTranslation",
     *   mappedBy="object",
     *   cascade={"persist", "remove"}
     * )
     */
    private $translations;


    public function __construct() 
    {
        $this->translations = new ArrayCollection();
    }

    public function getTranslations()
    {
        return $this->translations;
    }

    public function addTranslation(\TestModule\Entity\ArticleTranslation $t)
    {
        if (!$this->translations->contains($t)) {
            $this->translations[] = $t;
            $t->setObject($this);
        }
    }


    public function addTranslations($translations)
    {
        foreach ($translations as $translation) {
            $this->addTranslation($translation);
        }
    }

    public function removeTranslations($translations)
    {
        foreach ($translations as $translation) {
            $this->translations->removeElement($translation);
            $translation->setObject(null);
        }
    }


    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }

}

2)ArticleTranslation Entity

namespace TestModule\Entity;

use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;
/**
 * @Doctrine\ORM\Mapping\Entity
 * @Doctrine\ORM\Mapping\Table(name="test_module_articles_translations",
 *     uniqueConstraints={@Doctrine\ORM\Mapping\UniqueConstraint(name="lookup_unique_idx", columns={
 *         "locale", "object_id", "field"
 *     })}
 * )
 */
class ArticleTranslation extends AbstractPersonalTranslation
{
    /**
     * Convinient constructor
     *
     * @param string $locale
     * @param string $field
     * @param string $value
     */
    public function __construct($locale, $field, $value)
    {
        $this->setLocale($locale);
        $this->setField($field);
        $this->setContent($value);
    }
    /**
     * @Doctrine\ORM\Mapping\ManyToOne(targetEntity="TestModule\Entity\Article", inversedBy="translations")
     * @Doctrine\ORM\Mapping\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $object;
} 

3)表格

namespace TestModule\Form;

use Zend\Form\Form;
use Doctrine\ORM\EntityManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use TestModule\Form\ArticleTranslationsFieldset;
use TestModule\Entity\ArticleTranslation;

class ArticleForm extends Form
{
    protected $entityManager;

    public function __construct(EntityManager $entityManager,$name = null)
    {
        parent::__construct($name);

        $this->entityManager = $entityManager;

        $hydrator = new DoctrineHydrator($this->entityManager, 'TestModule\Entity\Article');


        $this->setAttribute('method', 'post')
            ->setHydrator($hydrator)
            //->setInputFilter($inputFilter)
        ;

        $this->add(array(
            'name'  => 'id',
            'type'  => 'Hidden',
        ));

        $articleFieldset = new ArticleTranslationsFieldset($entityManager);
        $fieldsetHydrator = new DoctrineHydrator($entityManager, 'TestModule\Entity\ArticleTranslation');
        $articleFieldset->setHydrator($fieldsetHydrator)->setObject(new ArticleTranslation('en','name',''));

        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'translations',
            'allow_empty' => true,
             'options' => array(
                 'label' => '',
                 'count' => 0,
                 'allow_add' => true,
                 'allow_remove' => true,
                 'target_element' => $articleFieldset,

             ),
         ));


        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Submit'
            ),
        ));


    }
}

4)和Translations Fieldset:

namespace TestModule\Form;

use Zend\Form\Fieldset;

class ArticleTranslationsFieldset extends Fieldset
{
    public function __construct()
    {

        parent::__construct('translations');

        $this->add(array(
            'name'  => 'locale',
            'type'  => 'Hidden',
        ));

        $this->add(array(
            'name'  => 'field',
            'type'  => 'Hidden',
        ));

        $this->add(array(
            'name' => 'content',
            'type' => 'Zend\Form\Element\Text',
            'options' => array(
                'label' => _(''),
            ),
            'attributes' => array(
                'type' => 'text',
            ),
        ));

    }

}

通过此设置,我可以保存每种语言的名称和描述属性,但我无法管理内容字段类型 - 它是名称和描述的Text元素,无法设置正确的字段标签。我也不能按语言对元素进行分组,以便呈现给用户的表单组织得很好。

您对如何解决此问题有任何其他建议吗? 我想要实现的是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

我无法找到我在问题中使用的可翻译学说扩展的解决方案。所以我搜索另一个,最后我最终使用Prezent扩展(https://github.com/Prezent/doctrine-translatable)。

通过此扩展,翻译实体包含可翻译字段,这使得翻译实体与翻译字段集的映射变得容易。每个翻译实体都有一个语言环境属性,我将其映射到字段集中的隐藏字段,并使用它以所需的方式显示表单。