2

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...

5
  • is the problem anywhere near return $repository->createQueryBuilder('t')->orderBy('t.id', 'ASC') ? Commented Oct 31, 2011 at 21:45
  • I found the problem, i will post it when 4 hours have passed. (can't self answer atm) Commented Oct 31, 2011 at 23:47
  • Congrats on finding a solution - was the problem on the Doctrine or Symfony side? Commented Nov 1, 2011 at 11:00
  • 1
    user side, it's always on the user side, right? :) Commented Nov 1, 2011 at 16:03
  • Definitely. Thx for posting your solution and welcome to SO Commented Nov 1, 2011 at 20:08

1 Answer 1

4

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.