public function saveUserTopics(SaveUserTopicsCommand $command)
{
if (empty($command->topics)) {
$query = $this->em->createQuery('DELETE Newscoop\\Entity\\UserTopic ut WHERE ut.user = :user');
$query->execute(array('user' => $command->userId));
} else {
$topics = $this->em->getRepository('Newscoop\\Entity\\UserTopic')->findByUser($command->userId);
foreach ($topics as $topic) {
if (in_array($topic->getTopicId(), $command->topics)) {
$this->em->remove($topic);
}
}
$this->em->flush();
}
$user = $this->em->getReference('Newscoop\\Entity\\User', $command->userId);
foreach ($command->selected as $topicId) {
$language = $this->em->getReference('Newscoop\\Entity\\Language', $command->languageId);
$topic = $this->em->getRepository('Newscoop\\NewscoopBundle\\Entity\\Topic')->getSingleTopicQuery($topicId, $language->getCode());
$this->em->persist(new UserTopic($user, $topic->getOneOrNullResult()));
}
$this->em->flush();
}