Category Archives: Doctrine

Event Listener (after login) on Symfony 2

If you need to do some kind of action after the user has logged-in on your system, you should add an event listener.

First, you need to create a service.

On your app/config.yml you must define your service:

my.service:
        class: YourApp\YourBundle\Listener\LoginListener
        arguments: [@doctrine]
        tags:
          - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }

After that you need to code your listener

<?php
 
namespace YourApp\YourBundle\Listener;
 
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
 
class LoginListener
{
    /**
     * @var Doctrine\Bundle\DoctrineBundle\Registry
     */
    private $doctrine;
 
    public function __construct(Doctrine $doctrine)
    {
        $this->doctrine = $doctrine;
    }
 
    public function onInteractiveLogin(InteractiveLoginEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();
 
        if ($user) {
            // your action with the current user
 
            $this->doctrine->getEntityManager()->persist($user);
            $this->doctrine->getEntityManager()->flush();
        }
    }
}

Dump update SQL From Doctrine 2 on Symfony 2

php app/console doctrine:schema:update --dump-sql

Create Single Entity on Doctrine 2 + Symfony 2

First if you don’t have an YML file but have table on your database, run this command to import YML file from this database:

php app/console doctrine:mapping:import YourAppBundle yml --filter="Table"

Based on an YML File this command will create a single entity for you:

php app/console doctrine:generate:entities YourAppBundle:Table --path src/

And if you want to create all entities

php app/console doctrine:mapping:import YourAppBundle yml
php app/console doctrine:generate:entities YourAppBundle

Reverse Engineer (Workbench, Doctrine, Symfony2) with annotation

Hi guys!!

First of all you need to create a data model on workbench and update your database

After your database is populated, run this:

php app/console doctrine:mapping:convert xml ./src/[YOUR/BUNDLE]/Resources/config/doctrine/metadata/orm --from-database --force

so run this:

php app/console doctrine:mapping:import [YourBundle] annotation

and this:

php app/console doctrine:generate:entities [YourBundle]
Page 1 of 11