Monthly Archives: January 2013

Running single test on phpunit

phpunit -c app/ --filter YourTestClass

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

Introduction

Hi,

Today I decided to write about some Design Patters I’m currently studying for my future reference and I hope it’ll be helpful for you too.

Each post on this post series will be based on books, websites and my own code. I’ll show each external reference I’ll use.

To make it simple I’ll use ClassLoader from Symfony 2 Framework (in a composer dependency, installed easy as “composer install” command)

Source code will be available on github.com.

These are the patterns I’m intending to write about:

- Specification
- Strategy
- Decorator
- Chain of Responsibility
- Facade
- Abstract Factory
- Factory Method
- Template Method
- Adapter
- Bridge
- Composite
- Proxy
- Mediator
- Observer
- Visitor

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
Page 1 of 11