symfony2实体字段类型错误

发布于 2024-12-13 06:13:16 字数 4455 浏览 0 评论 0原文

我还没有找到任何关于如何执行此操作的可靠示例。

我有我的实体 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱已欠费 2024-12-20 06:13:16

所以问题是我没有创建存储库。我创建了它(请参阅最后的代码),但仍然收到“必须管理传递到选择字段的实体”错误。

然后我将 mutiple 设置为 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();
    }
}

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:

$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
                    )
                );

I hope someone finds this useful, cause if I had had this, it would been easier.

Code for my repository:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文