Reputation: 2827
I have an entity Hall
which can either welcome another hall
(subhall), or a stand
.
I'm stuck with the hall
form (or at least it's what I think needs to be edited).
I can't figure out how to have it display a <select>
of all hall
with parent=0
I also need a default option which will be blank with 0
as value
Here are my files:
Hall Entity:
<?php
namespace SalonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Hall
*
* @ORM\Table(name="hall")
* @ORM\Entity(repositoryClass="SalonBundle\Repository\HallRepository")
*/
class Hall {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $idHall;
/**
* @var string
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var integer
* @ORM\Column(name="parent", type="integer", options={"default":0})
*/
private $parent;
/**
* @var
* @ORM\OneToMany(targetEntity="SalonBundle\Entity\Stand", mappedBy="hall")
*/
private $stand;
public function __construct()
{
$this->stand = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get idHall
* @return integer
*/
public function getIdHall()
{
return $this->idHall;
}
/**
* Set name
* @param string $name
* @return Hall
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set parent
* @param integer $parent
* @return Hall
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
* @return string
*/
public function getParent()
{
return $this->parent;
}
/**
* Add stand
* @param \SalonBundle\Entity\Stand $stand
* @return Hall
*/
public function addStand(\SalonBundle\Entity\Stand $stand)
{
$this->stand[] = $stand;
return $this;
}
/**
* Remove stand
* @param \SalonBundle\Entity\Stand $stand
*/
public function removeStand(\SalonBundle\Entity\Stand $stand)
{
$this->stand->removeElement($stand);
}
/**
* Get stand
* @return \Doctrine\Common\Collections\Collection
*/
public function getStand()
{
return $this->stand;
}
/**
* toString
* @return string
*/
public function __toString() {
return $this->getName();
}
}
Hall Form :
<?php
namespace SalonBundle\Form;
use SalonBundle\Entity\Hall;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class HallType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('parent') // Need to complete this part
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'SalonBundle\Entity\Hall'
));
}
}
How should I proceed ?
Upvotes: 5
Views: 2484
Reputation: 56
With the 'query_builder' option you can filter the entities:
http://symfony.com/doc/current/reference/forms/types/entity.html#ref-form-entity-query-builder
$builder->add('users', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
'choice_label' => 'username',
));
Upvotes: 4
Reputation: 4315
I guess you use Doctrine so try the EntityType:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
$builder->add('nom')
->add('description')
->add('parent', EntityType::class, [
'class' => 'SalonBundle:Hall',
'choice_label' => 'nom',
'choice_value' => 'idHall'
])
->add([...]);
}
Upvotes: 2