Deploying a Doctrine Entity Administrator to a Service for Deploying a Quick Notification Service

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.

+4
2

notifAction $this- > getUser();

$notificateur->notifier('your account is updated',$user);

, . , :

public function notifier($text, $user) {

    $notification=new Notification();
    $notification->setDate(new \DateTime());
    $notification->setText($text);
    $notification->setStatus('1');
    $notification->setUser($user);
    $this->em->persist($notification);
    $this->em->flush();
}        
+3

. php,

notifier() :

public function notifAction() {

        $user=$this->getUser();
        $users = array();
        array_push($users, $user);
        $notificateur=$this->get('notificateur');
        $notificateur->notifier('your account is updated',$users);
        Return new Response('OK');
}

, , - , :

$user=$this->getUser();
$users = array();
array_push($users, $user);

:

$repository = $this->getDoctrine()
->getRepository(.....)
$list_users=$repository->find(....)
//
//
//
$notificateur=$this->get('notificateur');
$notificateur->notifier('your account is updated',$list_users);
Return new Response('OK');

, , , symfony 2.

+1

All Articles