Sonata管理员使用a2lix_translatable。怎么做人际关系?

时间:2015-02-07 11:48:54

标签: symfony sonata-admin a2lix-translation

我想创建表Pracownik,它可以包含零个或几个Zaklady个实例,并使用Sonata Admin控制它。两个表都使用a2lix_translatable进行翻译。

所以我有课程:

<?php

namespace JCuryllo\InstituteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * Pracownik
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Pracownik
{

use ORMBehaviors\Translatable\Translatable;

public function __call($method, $arguments)
{
    return $this->proxyCurrentLocaleTranslation($method, $arguments);
}

/**
 * @ORM\ManyToOne(targetEntity="ZakladyTranslation")
 **/
private $zaklady;


/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;



/**
 *  and other properties
*/


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

   /* and other getters and setter */

}

翻译:

<?php

namespace JCuryllo\InstituteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * @ORM\Entity
 */
class PracownikTranslation
{
use ORMBehaviors\Translatable\Translation;

public function __toString()
{
    return $this->getTitle();
}


/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/* and other properties */

 /**
 * Set name
 *
 * @param string $name
 * @return Pracownik
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/* and other getters and setter *.

}

我在ZakladyZakladyTranslation中使用了非常相似的代码。然后在PracownikAdmin我尝试做某事:

protected function configureFormFields(FormMapper $formMapper)
{

    $formMapper
        ->add('zaklady', 'many_to_one',array(
         'required' => false,
         ))
        ->add('translations', 'a2lix_translations')

    ;
}

但它不起作用(错误:无法加载类型“many_to_one”)。

2 个答案:

答案 0 :(得分:0)

many_to_one不是Sonata的可用表单类型,您必须使用sonata_type_model或sonata_type_model_list来表示多对一关系。

示例:

protected function configureFormFields(FormMapper $formMapper)
{

    $formMapper
        ->add('zaklady', 'sonata_type_model_list',array(
            'required' => false,
        ))
        ->add('translations', 'a2lix_translations');
}

文档:http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-many-to-one

答案 1 :(得分:0)

好的,我已经完成了。

Zaklady我添加了:

public function __toString()
{
    return $this->getTitle();  // getTitle is in ZakladyTranslation!
}

Pracownik我改变了:

/**
 * @ORM\ManyToOne(targetEntity="ZakladyTranslation")
 **/
private $zaklady;

为:

/**
 * @ORM\ManyToOne(targetEntity="Zaklady")
 **/
private $zaklady;

PracownikAdmin

  protected function configureFormFields(FormMapper $formMapper)
{

    $formMapper

        ->add('translations', 'a2lix_translations')
        ->add('zaklady', 'sonata_type_model', array(
            'multiple' => true,
            'by_reference' => false,
            'required' => false

        ))


    ;
}