PrimuS
PrimuS

Reputation: 2683

Non-existing service in Symfony3

I have a class that I'd like to use as a service:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Config\Definition\Exception\Exception;

class Functions
{


private $em;

public function setEntityManager(EntityManager $em)
{
    $this->em = $em;
}

/**
 * Init the add Event to Eventlog function
 *
 * @param string $text Text to save
 * @param User $user
 *
 * @return boolean
 */
public function addEvent($text, User $user)
{

    $event = new Event();
    $event->setUser($user);
    $event->setText($text);
    $event->setEventTimestamp(new \DateTime());
    $this->em->persist($event);

    try{
        $this->em->flush();
    }catch (Exception $e){
        return false;
    }

    return true;

}

}

I inject the service via services.yml like this:

services:
    app.ccrm:
        class: AppBundle\Entity\Functions
        calls:
            - [setEntityManager, ["@doctrine.orm.default_entity_manager"]]

and make the call with this in my controller:

$this->get('app.ccrm')->addEvent('Event IDXXX',$this->getUser());

No it gives me the

You have requested a non-existent service "app.ccrm".

Things I did:

Now I am all out of ideas! Any hints?

Upvotes: 2

Views: 399

Answers (1)

lordrhodos
lordrhodos

Reputation: 2745

check the output of php bin/console debug:container app.ccrm again, and look at the public: no line. You need to set your service to be public.

services:
    app.ccrm:
        class: AppBundle\Entity\Functions
        public: true
        calls:
            - [setEntityManager, ["@doctrine.orm.default_entity_manager"]]

Additionally, check the documentation about default service visibility

Upvotes: 1

Related Questions