Get all duplicate records in table

Slow variant:

FROM table t1
WHERE t1.domain_id = 1569
AND t1.is_active = 1
AND (t1.field IN (SELECT t2.field
                 FROM table t2
                 WHERE t2.is_active = 1
                 GROUP BY t2.field
                 HAVING COUNT(t2.field) > 1))

Faster variant:

SELECT l0_.*
FROM table l0_
inner join table n2 on n2.field = l0_.field
where <>
AND l0_.is_active = 1
Group By


    ->innerJoin(Table::class,'table', Query\Expr\Join::WITH, 'entity.field = table.field')
    ->andWhere(' <>')
    ->andWhere('table.isActive = 1')

Nested Queries with Query Builder

$queryBuilder = $this->createQueryBuilder('account_event');
            ->select(' AS AE_id', 'IDENTITY(account_event.event) AS event_id', 'account_event.dueDate as due_date', 'IDENTITY(account_event.account) AS account_id')
                        ->andWhere('ae2.event IN (:events)')
                        ->andWhere('ae2.account IN (:accounts)')
                        ->andWhere('ae2.dueDate > DATE(:date)')
            ->setParameter('events', $events)
            ->setParameter('accounts', $accounts)
            ->setParameter('date', $date->format('Y-m-d'))

Fast Setup Virtual Machine and Symfony

sudo dpkg --configure -a
sudo apt-get install mysql-server
apt-cache pkgnames | grep php7.3
php -r "copy('', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

# run this if you are building a traditional web application

composer create-project symfony/skeleton ./

# run this if you are building a microservice, console application or API

composer create-project symfony/website-skeleton ./

Current folder should be empty!!!

Continue reading “Fast Setup Virtual Machine and Symfony”

RabbitMQ Work Queue with Symfony

Add to .env


Add to services.yaml

        arguments: ['%env(RABBITMQ_HOST)%', '%env(RABBITMQ_PORT)%', '%env(RABBITMQ_USERNAME)%', '%env(RABBITMQ_PASSWORD)%', '%env(RABBITMQ_QUEUE_NAME)%']

            arguments: ['%env(RABBITMQ_HOST)%', '%env(RABBITMQ_PORT)%', '%env(RABBITMQ_USERNAME)%', '%env(RABBITMQ_PASSWORD)%', '%env(RABBITMQ_QUEUE_NAME)%']

Continue reading “RabbitMQ Work Queue with Symfony”

Symfony – Create a service – the right way

Always create service interface for your service. This way you will have better structured code and phpunit testing will be real pleasure.
Also use dependency injection for your repositories in the service.

Service Interface Example

interface UserServiceInterface{
  public function getUsername();
  public function viewAll();

Service Example

class UserService implements UserServiceInterface

  private $entityManager;
  private $userRepository;

  public function __construct(EntityManager $entityManager, UserRepository $userRepository)
    $this->entityManager = $entityManager;
    $this->userRepository = $userRepository;

  public function viewAll()
    return $this->userRepository->findAll();

Repository Example

class UserRepository extends \Doctrine\ORM\EntityRepository
  public function __construct(EntityManager $em, Mapping\ClassMetadata $metadata = null)
      $metadata == null ?
      new Mapping\ClassMetadata(User::class) :

Symfony Security Authorization

There are several way to make authorization on your site with Symfony.
First is in your security.yml on the same level as firewalls tag:

        - { path: ^/admin, roles: ROLE_USER }

This will block all users that don’t have ‘ROLE_USER’ to admin page.

Second option is to use Controller. Inside Method insert:

            throw $this->createAccessDeniedException('GET OUT!');

Or like this:


This will deny access to all users that don’t have ‘ROLE_ADMIN’ to this method.

You can also use annotations:

* @Security("is_granted('ROLE_ADMIN')")

Annotations also work if you add them to the Class. So they will block all users that don’t have ‘ROLE_ADMIN’ to this class.

Continue reading “Symfony Security Authorization”

Symfony Security Authentication

Create Entity named User


namespace AppBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

 * @ORM\Entity
 * @ORM\Table(name="user")
class User implements UserInterface
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
    private $id;

     * @ORM\Column(type="string", unique=true)
    private $email;

     * @ORM\Column(type="string")
    private $password;

    private $plainPassword;

    public function getUsername()
        return $this->email;

    public function getRoles()
        return ['ROLE_USER'];

    public function getPassword()
        return $this->password;

    public function getSalt()

    public function eraseCredentials()
        $this->plainPassword = null;

     * @param mixed $email
    public function setEmail($email)
        $this->email = $email;

     * @param mixed $password
    public function setPassword($password)
        $this->password = $password;

     * @return mixed
    public function getPlainPassword()
        return $this->plainPassword;

     * @param mixed $plainPassword
    public function setPlainPassword($plainPassword)
        $this->plainPassword = $plainPassword;
        //This should be here to set value for password to activate doctrine listeners
        $this->password = null;


and generate migration:

#php bin/console doctrine:migrations:diff
#php bin/console doctrine:migrations:migrate

Continue reading “Symfony Security Authentication”