I created a form with one checkbox.
UserSettingsType.php:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('newsletter', 'checkbox', array(
'label' => 'Newsletter erhalten',
'attr' => array(
'class' => 'form-control',
),
'required' => false,
));
}
In the UserSettings.php Entity:
/**
* @ORM\Column(name="newsletter", type="boolean")
*/
protected $newsletter;
In the User.php:
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $user_settings_id;
/**
* @ORM\OneToOne(targetEntity="UserSettings", cascade={"persist"})
* @ORM\JoinColumn(name="user_settings_id", referencedColumnName="id")
*/
protected $settings;
In the PageController.php i handle the settings action:
public function settingsAction() {
$user = $this->getUser();
if ($user->getSettings() !== null) {
$settings = $user->getSettings();
} else {
$settings = new UserSettings($user);
}
$settings_form = $this->createForm(new UserSettingsType(), $settings);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$em = $this->getDoctrine()->getManager();
$settings_form->bind($request);
if ($settings_form->isValid()) {
$user->setSettings($settings);
$em->persist($user);
$em->flush();
}
}
return $this->render('MyCompMyAppBundle:Page:settings.html.twig', array(
'settings_form' => $settings_form->createView(),
));
}
I want to change the checkbox values from false (unchecked) / true (checked) to 'no' / 'yes' and change the definition of the newsletter field to: * @ORM\Column(name="newsletter", type="string", columnDefinition="ENUM('yes', 'no')") It would be nice if there would be 'yes' and 'no' enum values in the database. Please correct me if i am wrong: There is no way to change this via form element attributes, right? I heard something about a DataTransformer:. But is there any easier way to realize this?