Nested CollectionType Form

Example of nested collection forms
TemplateType.php

class TemplateType extends AbstractType
{


    public function buildForm(FormBuilderInterface $builder, array $options): void
    {

        $builder
            ->add('name', TextType::class, [
                'label' => "Template Name",
                'required' => true,
                'attr' => [
                    'class' => 'form-control',
                ],
            ])
            ->add('sentences', CollectionType::class, [
                'entry_type' => SentenceType::class,
                'entry_options' => ['label' => false],
                'label' => false,
                'allow_add' => true,
                'allow_delete' => true,
                'delete_empty' => true,
                'by_reference' => false,
                'prototype' => true,
                'prototype_name' => '__sentence__',
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Update',
                'attr' => [
                    'class' => "btn btn-sm btn-primary"
                ],
            ],
            )
        ;

    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Template::class,
            'csrf_protection' => false,
        ]);
    }

Continue reading “Nested CollectionType Form”

Get all duplicate records in table

Slow variant:

SELECT *
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 l0_.id <> n2.id
AND l0_.is_active = 1
Group By l0_.id

QueryBuilder:

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

Nested Queries with Query Builder

$queryBuilder = $this->createQueryBuilder('account_event');
        $queryBuilder
            ->select('account_event.id AS AE_id', 'IDENTITY(account_event.event) AS event_id', 'account_event.dueDate as due_date', 'IDENTITY(account_event.account) AS account_id')
            ->where(
                $queryBuilder->expr()->in(
                    'account_event.id',
                    $this->createQueryBuilder('subquery_repository')
                        ->select('MAX(ae2.id)')
                        ->from(AccountEvent::class,'ae2')
                        ->andWhere('ae2.event IN (:events)')
                        ->andWhere('ae2.account IN (:accounts)')
                        ->andWhere('ae2.dueDate > DATE(:date)')
                        ->groupBy('ae2.account')
                        ->getDQL()
                )
            )
            ->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('https://getcomposer.org/installer', '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 ./ 

OR
# 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

RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USERNAME=test
RABBITMQ_PASSWORD=test
RABBITMQ_QUEUE_NAME=task_queue

Add to services.yaml

App\Command\WorkerCommand:
        arguments: ['%env(RABBITMQ_HOST)%', '%env(RABBITMQ_PORT)%', '%env(RABBITMQ_USERNAME)%', '%env(RABBITMQ_PASSWORD)%', '%env(RABBITMQ_QUEUE_NAME)%']

    App\Command\PublisherCommand:
            arguments: ['%env(RABBITMQ_HOST)%', '%env(RABBITMQ_PORT)%', '%env(RABBITMQ_USERNAME)%', '%env(RABBITMQ_PASSWORD)%', '%env(RABBITMQ_QUEUE_NAME)%']

Continue reading “RabbitMQ Work Queue with Symfony”

Setting up RabbitMQ

First, we need to use composer to install RabbitMQ Server and the PHP package for interfacing with RabbitMQ.

composer require php-amqplib/php-amqplib

sudo apt-key adv --keyserver "hkps.pool.sks-keyservers.net" --recv-keys "0x6B73A36E6026DFCA"
wget -O - "https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc" | sudo apt-key add -
sudo apt-get install apt-transport-https
sudo apt-get update -y
sudo apt-get install -y erlang
sudo apt-get update
sudo apt-cache policy
sudo apt-get install -y rabbitmq-server

Create user for rabbitMQ and set permissions
only root can create user

sudo su root 
rabbitmqctl add_user test test
rabbitmqctl set_user_tags test administrator
rabbitmqctl set_permissions -p / test ".*" ".*" ".*"

rabbitmq-plugins enable rabbitmq_management

You can now reach RabbitMQ interface at http://localhost:15672

Install MongoDB in Ubuntu

Create a bash file named install_mongo.bash:

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse$
apt-get update
apt-get install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod

Execute the bash file

sudo bash ./install_mongo.bash

You are ready to use Mongo