K
K
Kerm2020-07-10 16:46:47
symfony
Kerm, 2020-07-10 16:46:47

Symfony 3.3 don't understand how to properly call a service function?

Sorry, I'm terribly stupid.

There is a service:

namespace AppBundle\Services;

use Doctrine\DBAL\Connection;

class Permission extends BaseService
{
    /**
     * @var Connection
     */
    private $connection;


    /**
     * Permission constructor.
     * @param mixed ...$args
     */
    public function __construct(...$args)
    {
        parent::__construct(...$args);
        $this->connection = $this->em->getConnection();
    }

    /**
     * @param string $role_name
     * @return array
     */
    public function getPermissionsByRoleName(string $role_name): array
    {
        $q = $this->connection->prepare('SELECT * FROM `permissions` WHERE `roleName` = ":roleName";');
        $q->bindValue('roleName', $role_name);
        $q->execute();

        return $q->fetchAll();
    }
}


It extends the class:

namespace AppBundle\Services;

use Doctrine\Bundle\DoctrineBundle\Registry;

class BaseService
{
    /**
     * @var Registry
     */
    protected $em;
    /**
     * @var Configure
     */
    protected $config;
    /**
     * @var \AppKernel
     */
    protected $kernel;

    /**
     * Configure constructor.
     * @param Registry $em
     * @param Configure $configure
     * @param \AppKernel $kernel
     */
    public function __construct(Registry $em, Configure $configure, \AppKernel $kernel)
    {
        $this->em = $em;
        $this->config = $configure;
        $this->kernel = $kernel;
    }
}


I need to call the getPermissionsByRoleName() method, the problem is that I don’t know what I need to pass when calling the class to make it work, there are three arguments in the constructor, and no matter how I try, I can’t figure out how to correctly and what needs to be passed ...

I tried to call the class like this: but still the error is that I don’t pass anything when calling the class (
$i = new Permission(new Registry, new Configure, new \AppKernel);
$i->getPermissionsByRoleName('....');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Deniev, 2020-08-06
@deniev

I'm working on 4.4, in my case I would call in the controller like this:

/**
* @Route("/", name="app_home", methods={"GET"})
* @param Permission $permission
*/
public function index(Permission $permission)
{
    $permission->getPermissionsByRoleName('role_name');
}

If I would call it in another service or in a form, etc., then through __construct:
private $permisson;

public function __construct(Permission $permission)
{
   $this->permisson = $permission;
}

public function myFunc()
{
    $this->permisson->getPermissionsByRoleName('role_name');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question