symfony2实体字段类型错误
我还没有找到任何关于如何执行此操作的可靠示例。
我有我的实体 Shield,它可以有超过 1 个 ShieldType。我想做的是,创建一个表单,创建一个具有不同屏蔽类型的新屏蔽。
这是我的代码,老实说,我不知道我的错误在哪里:
错误是:
Entities passed to the choice field must be managed
500 Internal Server Error - FormException
Armory\SearchBundle\Entity\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');
}
}
Armory\SearchBundle\Entity\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)
谢谢...
I haven't found any solid example on how to do this.
I have my entity Shield, which can have more than 1 ShieldTypes. What I want to do is, create a form that creates a new Shield with different Shieldtypes.
This is my code, I honestly don't know where is my error:
Error is:
Entities passed to the choice field must be managed
500 Internal Server Error - FormException
Armory\SearchBundle\Entity\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');
}
}
Armory\SearchBundle\Entity\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;
}
}
It can be a little confusing but:
Shield entity (database)
ShieldTypes entity (database)
ShieldType form (abstracttype)
Thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以问题是我没有创建存储库。我创建了它(请参阅最后的代码),但仍然收到“必须管理传递到选择字段的实体”错误。
然后我将 mutiple 设置为 true:
我希望有人发现这很有用,因为如果我有这个,那就更容易了。
我的存储库的代码:
So the problem was that I didn't create a repository. I created it (see code at the end) and I still got 'Entities passed to the choice field must be managed' error.
Then I set mutiple to true in:
I hope someone finds this useful, cause if I had had this, it would been easier.
Code for my repository: