2011-10-31 123 views
2

我還沒有找到關於如何做到這一點的任何可靠的例子。symfony2實體字段類型錯誤

我有我的實體盾牌,它可以有一個以上的盾牌類型。我想要做的是創建一個表單,用不同的Shieldtypes創建一個新的Shield。

這是我的代碼,我真的不知道哪裏是我的錯誤:

錯誤是:

Entities passed to the choice field must be managed 
500 Internal Server Error - FormException 

軍械庫\ SearchBundle \實體\ Shield.php

namespace Armory\SearchBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
/** 
* Armory\SearchBundle\Entity\Shield 
* 
* @ORM\Table(name="shield") 
* @ORM\Entity 
*/ 
class Shield 
{ 
    /** 
    * @var integer $id 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 
    /** 
    * @var string $name 
    * @ORM\Column(name="name", type="string", length=45, nullable=false) 
    */ 
    private $name; 
    /** 
    * @var integer $defense 
    * @ORM\Column(name="defense", type="integer", nullable=false) 
    */ 
    private $defense; 

    /** 
    * @var boolean $active 
    * @ORM\Column(name="active", type="boolean", nullable=false) 
    */ 
    private $active; 

    /** 
    * @ORM\ManyToMany(targetEntity="ShieldTypes") 
    * @ORM\JoinTable(name="i_orbs_type", 
    *  joinColumns={@ORM\JoinColumn(name="id_orb", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="id_type", referencedColumnName="id")} 
    *) 
    * @var ArrayCollection $types 
    */ 
    protected $types; 
    public function __construct(){ 
     $this->types = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 
    public function getTypes(){ 
     return $this->types; 
    } 
    public function getId() 
    { 
     return $this->id; 
    } 
    public function setName($name) 
    { 
     $this->name = $name; 
    } 
    public function getName() 
    { 
     return $this->name; 
    } 
    public function setDefense($defense) 
    { 
     $this->defense = $defense; 
    } 
    public function getDefense() 
    { 
     return $this->defense; 
    } 
    public function setActive($active) 
    { 
     $this->active = $active; 
    } 
    public function getActive() 
    { 
     return (bool)$this->active; 
    } 
} 

Armory \ SearchBundle \ Form \ ShieldType.php:

namespace Armory\SearchBundle\Form; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
class ShieldType extends AbstractType{ 

public function buildForm(FormBuilder $builder, array $options){ 
    $builder->add('name','text'); 
    $builder->add('defense','integer'); 
    $builder->add('active','checkbox'); 

    $builder->add('types','entity', 
      array(
       'class'=>'Armory\SearchBundle\Entity\ShieldTypes', 
       'query_builder'=> function($repository){ 
          return $repository->createQueryBuilder('t')->orderBy('t.id', 'ASC'); 
        }, 
       'property'=>'name',) 
      ); 
} 

public function getName(){ 
    return 'shield'; 
} 

public function getDefaultOptions(array $options){ 
    return array('data_class'=>'Armory\\SearchBundle\\Entity\\Shield'); 
} 
} 

軍械庫\ SearchBundle \實體\ ShieldTypes.php

namespace Armory\SearchBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
/** 
* SOA\CRMBundle\Entity\ShieldTypes 
* 
* @ORM\Table(name="orbs_type") 
* @ORM\Entity 
*/ 
class ShieldTypes 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 
    /** 
    * @var string $name 
    * 
    * @ORM\Column(name="name", type="string", length=45, nullable=false) 
    */ 
    private $name; 
    /** 
    * @var string $title 
    * 
    * @ORM\Column(name="title", type="string", length=45, nullable=false) 
    */ 
    private $title; 
    public function getId() 
    { 
     return $this->id; 
    } 
    public function setName($name) 
    { 
     $this->name = $name; 
    } 
    public function getName() 
    { 
     return $this->name; 
    } 
    public function setTitle($title) 
    { 
     $this->title = $title; 
    } 
    public function getTitle() 
    { 
     return $this->title; 
    } 
} 

它可以是一個有點混亂,但:

Shield entity (database) 
ShieldTypes entity (database) 
ShieldType form (abstracttype) 

謝謝...

+0

的問題是附近的任何地方返回$ repository-> createQueryBuilder( 'T') - >排序依據( 't.id', 'ASC')? – jonv1

+0

我發現了這個問題,我會在4個小時過去後發佈它。 (不能自己回答atm) – jonv1

+0

恭喜找到解決方案 - 是在教條或Symfony方面的問題? – cantera

回答

4

所以,問題是,我沒有」創建一個存儲庫。我創建了它(最後見代碼),我仍然得到'傳遞給選擇字段的實體必須被管理'錯誤。

然後我設置多發爲true:

$builder->add('types','entity', 
       array(
        'class'=>'Armory\SearchBundle\Entity\ShieldTypes', 
        'query_builder'=> function(\Armory\SearchBundle\Entity\Repository\ShieldTypesRepository $repository){ 
            return $repository->createQueryBuilder('s')->orderBy('s.id', 'ASC');}, 
        'property'=>'title', 
        'multiple'=>true 
        ) 
       ); 

我希望有人認爲這是有用的,因爲如果我有過這一點,它會如此簡單。

代碼爲我的倉庫:

namespace Armory\SearchBundle\Entity\Repository; 
use Doctrine\ORM\EntityRepository; 

class ShieldTypesRepository extends EntityRepository 
{ 
    public function getAll() 
    { 
     return $this->_em->createQuery('SELECT s FROM Armory\SearchBundle\Entity\ShieldTypes s') 
         ->getResult(); 
    } 
}