Category Archives: Symfony2

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

Install less and Configure on Symfony 2 on Ubuntu 12.04

This also can fix the following errors when parsing a less file with node:
- Error: Cannot find module ‘less’
- TypeError: Cannot call method ‘charAt’ of undefined

First you need to install npm (https://npmjs.org/ Package manager for node)

sudo apt-get install npm

Install less (latest version)

sudo npm install --global less

Now configure your app/config.yml

..
assetic:
    ...
    filters:
        ...
        less:
            ...
            node_paths: [/usr/local/lib/node_modules] (or /usr/lib/node_modules)

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]

Schema update (Symfony2 + Doctrine)

php app/console doctrine:schema:update --force [--env="[ENV]"]

Generate Entities (Symfony2 + Doctrine)

php app/console doctrine:generate:entities [BUNDLE]/Entity/[ENTITY]

FIX Symfony2 vendor management GIT clone error

rror: RPC failed; result=18, HTTP code = 200MiB | 20 KiB/sfatal: The remote end hung up unexpectedlyfatal: early EOFfatal: index-pack failedwarning: http unexpectedly said: ’0000′

edit ‘deps’ file

change http to https

i.e.:

[symfony]
git=http: ….

[symfony]
git=https: ….

Page 1 of 11