I'm new to symfony 2, browsing the documentation, and I'm struggling to create a notification service to notify the list of users of some updates (the user object is in relation to OneToMany with the notification object, just to make it clear)
This is a class of service:
<?php
namespace OC\UserBundle\Services;
use OC\UserBundle\Entity\Notification;
use Doctrine\ORM\EntityManager as EntityManager;
class Notificateur
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function notifier($text, $users)
{
foreach ($users as $user)
{
$notification=new Notification();
$notification->setDate(new \DateTime());
$notification->setText($text);
$notification->setStatus('1');
$notification->setUser($user);
$this->em->persist($notification);
}
$this->em->flush();
}
}
this is how i defined my service in service.yml inside my package:
services
notificateur:
class: OC\UserBundle\Services\Notificateur
arguments: [ @doctrine.orm.entity_manager ]
and this action is inside my controller (this is just for the test, to notify the current user:
public function notifAction() {
$user=$this->getUser();
$notificateur=$this->get('notificateur');
$notificateur->notifier('your account is updated',$user);
Return new Response('OK');
}
when I run app / console debug: container, I can see my service there, but nothing is stored in the database. I do not know what I am missing, I would appreciate it if you could help me with this.