Reputation: 75
I am creating a select that takes the data of an entity, called category.
The select that I want to develop, would basically be the same that I have developed and working, but with the values I take from the category entity.
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use BackendBundle\Entity\Categoria;
use BackendBundle\Entity\CategoriaRepository;
class ProductoType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nombre', TextType::class, array(
'label' => 'Nombre',
'required' => 'required',
'attr' => array(
'class' => 'form-name form-control'
)
))
->add('categoria', ChoiceType::class, array(
'choices' => array(
'General' => '1',
'Coffe' => '2'
),
'required' => false,
'empty_data' => null
))
->add('Save', SubmitType::class, array(
"attr" =>array(
"class" => "form-submit btn btn-success"
)
))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BackendBundle\Entity\Producto'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'backendbundle_producto';
}
}
I would add a section like the following, but I get the error Could not load type "entity"
->add('categoria', 'entity', array(
'class' => 'BackendBundle:Categoria'
)
)
The original BBDD is documented in Object of class \BackendBundle\Entity\Categoria could not be converted to string
Upvotes: 0
Views: 650
Reputation: 2030
Firstly if your are using symfony 3 you must use Symfony\Bridge\Doctrine\Form\Type\EntityType
and the class should be the class name not the entity name
->add('categoria', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', array(
'class' => 'BackendBundle\Entity\Categoria'
)
)
and categoria should look like:
namespace BackendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity()
*/
class Categoria
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string")
*/
protected $name;
public function __toString()
{
return $this->name;
}
}
Upvotes: 1
Reputation: 1491
'entity' should be EntityType::class, you should use the classname of the EntityType instead of just the 'entity' string
See: https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md#form
Upvotes: 1