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)
謝謝...
的問題是附近的任何地方返回$ repository-> createQueryBuilder( 'T') - >排序依據( 't.id', 'ASC')? – jonv1
我發現了這個問題,我會在4個小時過去後發佈它。 (不能自己回答atm) – jonv1
恭喜找到解決方案 - 是在教條或Symfony方面的問題? – cantera