0

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.

Community
  • 1
  • 1
Ryall
  • 12,010
  • 11
  • 53
  • 77
  • 1
    What you want to do is to add a FormEvents::PRE_SET_DATA event to your NotificationSettingType. This will allow checking your check boxes based on data inside of your UserNotification entity. http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html – Cerad Dec 24 '14 at 00:41
  • And while a bit off topic, if you have 15+ boolean columns then you really should consider using a OneToMany relation between User and UserNotification. As it stands right now, you won't be able to add additional notifications without changing your entity. – Cerad Dec 24 '14 at 00:45
  • Thanks, I'll take a look but why is this a comment not an answer? Also, to add additional notifications it requires code changes anyway, as those notifications need to be implemented. A one to many provides no benefits that I can see, and makes the code more complicated. – Ryall Dec 24 '14 at 10:42
  • Too lazy to write up a real answer. As far as the relation goes, it was just a suggestion. I would start with an configuration array for defining the notifications. Once implemented, additional ones could be added by adjusting the configuration. Bit a more work upfront perhaps but might save a bunch of boiler plate code. Again, just a suggestion. – Cerad Dec 24 '14 at 14:36

0 Answers0