I'm currently implementing notification preferences on a site using Symfony 2.6.
The entity that holds the notification preference is as follows:
class UserNotification
{
//...
/**
* @ORM\Column(type="boolean")
*/
private $downtimeNotificationEnabled;
/**
* @ORM\Column(type="boolean")
*/
private $newPostNotificationEnabled;
//...
}
There are currently about 15 different keys like this, but I would like now to also allow configuration of Email, SMS and Dashboard notifications for each type. Rather than create two additional columns for each type, I was thinking to change each $*Enabled
column type from boolean
to Doctrine array
, and hold the data as an associative array (['dashboard' => true, 'email' => false, 'sms' => true]
) within each table column.
My question is in regards to then hooking this array
column into a Symonfy Form type. From what I understand, I can create a custom type for the field, and use this instead of the 'checkbox'
type I currently use. Something like this:
class NotificationSettingType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dashboard', 'checkbox', [
'mapped' => false,
'data' => true
])
->add('email', 'checkbox', [
'mapped' => false,
'data' => true
])
->add('sms', 'checkbox', [
'mapped' => false,
'data' => true
])
;
}
//...
}
...and the main form type...
class NotificationsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('downtimeNotificationEnabled', new NotificationSettingType())
->add('newPostNotificationEnabled', new NotificationSettingType())
//...
;
}
}
But then I don't understand how I can populate the values of the 'checkbox'
types in the NotificationSettingType
with the array
data from the entity.
Edit 1
To further clarify:
I'm looking for how to bind the data from/to the columns in UserNotification
specifically.
For example, say:
UserNotification::$downtimeNotificationEnabled = [
'dashboard' => true,
'email' => false,
'sms' => true
]
Then in NotificationSettingType
for this column:
'dashboard'
would be checked'email'
would be unchecked'sms'
would be checked
Then, after submitting, if I unchecked SMS for example, the new array would be in the column variable as:
UserNotification::$downtimeNotificationEnabled = [
'dashboard' => true,
'email' => false,
'sms' => false
]
I know how to pass options but I don't know how to transform this data to make it compatible with the Symfony2 form types.