/** * Updates or creates a iser preference if it does not exist * @param \ClassCentral\SiteBundle\Entity\User $user * @param $type * @param $value */ public function updatePreference(\ClassCentral\SiteBundle\Entity\User $user, $type, $value) { $em = $this->container->get('doctrine')->getManager(); if (!in_array($type, UserPreference::$validPrefs)) { throw new \Exception("Preference {$type} is not a valid preference"); } $prefMap = $user->getUserPreferencesByTypeMap(); if (in_array($type, array_keys($prefMap))) { // Update the preferences $up = $prefMap[$type]; $up->setValue($value); $em->persist($up); } else { // Create the preferences $up = new UserPreference(); $up->setUser($user); $up->setType($type); $up->setValue($value); $em->persist($up); } $em->flush(); return true; }