I have the segments checkbox in my form and when I submit, these values doesn't appears in post's return. The segment isn't mapped because it's a external value from database.
/**
* Users
*
* @ORM\Table(name="users", indexes={@ORM\Index(name="fk_users_users_groups1_idx", columns={"users_groups_id"})})
* @ORM\Entity
*/
class Users
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=45, nullable=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=32, nullable=true)
*/
private $password;
/**
* @var boolean
*
* @ORM\Column(name="active", type="boolean", nullable=true)
*/
private $active;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var UsersGroups
*
* @ORM\ManyToOne(targetEntity="UsersGroups", cascade={"all"}, fetch="EAGER")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="users_groups_id", referencedColumnName="id")
* })
*/
private $usersGroups;
public function __construct()
{
$this->usersGroups = new ArrayCollection();
}
class UsersType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => 'Nome',
'attr' => array(
'class' => 'form-control'
)
))
->add('email', 'text', array(
'label' => 'E-mail',
'attr' => array(
'class' => 'form-control'
)
))
->add('password', 'password', array(
'label' => 'Senha',
'attr' => array(
'class' => 'form-control'
)
))
->add('usersGroups', 'entity', array(
'class' => 'Ad\SisBundle\Entity\UsersGroups',
'label' => 'Grupo do usuário',
'attr' => array(
'class' => 'form-control'
)
))
->add('active', 'checkbox', array(
'label' => 'Ativo',
'attr' => array(
'class' => 'form-control'
)
))
->add('segments', 'entity', array(
'class' => 'Ad\SisBundle\Entity\Segments',
'query_builder' => function( EntityRepository $segments ) {
return $segments->createQueryBuilder("s");
},
'multiple' => true,
'mapped' => false,
'expanded' => true,
))
;
}
And this is the post's result
Users {#2011 ▼
-name: "xxx"
-email: "xxx@hotmail.com"
-password: "312321"
-active: true
-createdAt: null
-updatedAt: null
-id: 1
-usersGroups: UsersGroups {#2013 ▶}
}
I've tried this:
$segments = $editForm->get('segments')->getData();
I believe that if the segment information was the object of users, it would be easier to handle the information.
This is the right way to do this? How can I get the segment data?
If the segments are not mapped it won't passed to the entity. you can either get it from the request in a preSubmit event subscriber(look at http://symfony.com/doc/current/components/form/form_events.html#event-subscribers) or , changing mapped to true, and pass it to the entity but leave the field of the entity without the ORM annotation if you don't want it to be persisted.