0

我想创建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 *.

}

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

protected function configureFormFields(FormMapper $formMapper)
{

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

    ;
}

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

4

2 回答 2

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

于 2015-02-07T11:56:13.170 回答
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

        ))


    ;
}
于 2015-02-07T12:36:12.177 回答