Save user id automatically in entities with Doctrine

0

I use Doctrine 2 to map my system database. Most entities have a foreign key pointing to an "account" entity.

When a user logs on to the system, I add his account id to a session variable. Next I add a SQLFilter in the Doctrine (bootstrap) settings like this:

$config->addFilter("conta", "\Classes\FiltroConta");

This statement adds a condition to every query the system makes, something like

SELECT nome FROM pessoas WHERE nome = 'pedro' AND conta = 1

The condition conta = 1 is added automatically whenever the filter is configured. And the number 1 is the id that is in the session variable.

Imagine that to persist a given in the entity people I need to do the following statement:

//Obtem referencia da entidade conta
$conta = $em->getReference('Conta', $SESSION['idConta']);
$pessoa = new pessoas();
$pessoa->setNome('Pedro');
$pessoa->conta($conta);
$em->persisty($pessoa);
$em->Flush();

What I'm looking for is a way to reference the entity account automatically, using something native to Doctrine, in the same way that SQLFilter does in queries.

Does anyone know anything?

    
asked by anonymous 17.04.2017 / 21:12

1 answer

0

After persist Doctrine populates the entity id:

$em->persist($account);
$em->flush();
$account->getId();

The $account->getId() will return the generated id for you.

    
17.04.2017 / 21:58