'Symfony 5 - Inject Security service in Entity doesn't work

I try to get security service in an Entity. When i want to access it in my entity, the property "$this->security" is null

enter image description here

See the entity :

<?php

namespace App\Entity\Production;

use Symfony\Component\Security\Core\Security;

/**
 * @ORM\Entity(repositoryClass=MarqueRepository::class)
 * @ORM\HasLifecycleCallbacks()
 */
class Marque
{
    /* Others properties useless in the stackoverflow question*/

    /**
     * @var Security
     */
    private $security;

    public function __construct(Security $security)
    {
        $this->security = $security;
        dd($this->security);
    }

}
                

Autowiring is active in "services.yaml". I removed the folder "Entity" in the "exclude src" enter image description here

Could you help me ? Thxs



Solution 1:[1]

Bad practice. As said in comment by Lunin Roman and Mcsky, security check could be made in service/controller/etc.

Solution 2:[2]

I was read some commentaries about this bad practice, unless that you just need retrive the currently user id logged. Then it is my case.

class Aaaa
   
    private $security;

    public function __construct(Security $security)
    {
        $this->security = $security;
        
    }
}

services.yaml
services:
# default configuration for services in *this* file
_defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
    resource: '../src/'
    exclude:
        - '../src/DependencyInjection/'
        - '../src/Kernel.php'
        - '../src/Tests/

so i got this error..

Cannot create an instance of App\Entity\Aaaaa from serialized data because its constructor requires parameter "security" to be present.

Could by config error?

Solution 3:[3]

Technically, the answer ist yes. You can do the following:

create or replace function committest return number as 
begin 
  update my_table set col = 'x';
  commit;
  return 1;
end;
/

declare
  number n;
begin
  n := committest();
end;
/

However, you can't do the following:

select committest() from dual;

this would be a commit during a query and thus result in a

ORA-14552: Cannot Perform a DDL Commit or Rollback Inside a Query or DML

Solution 4:[4]

Yes, you can do that if you make the function an autonomous transaction. That way it will not be part of the current transaction anymore.

create or replace function doIt
as
  pragma autonomous_transaction;
begin
  ... code ...
  commit;
end;
/

More documentation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Captain Nemo
Solution 2 Daniel Cortizo
Solution 3 HAL 9000
Solution 4 Martin Schapendonk