When you are using Symfony PHP Framework, the most common way to interact with Databases is using Doctrine. And if you are using a relational Database like MySql or MariaDb, you will need to handle Table Associations. One of the most common ones is the Many to Many association, that means "Many of these, can have Many of those".

You may refer to this posts:

 

Lets assume we have two entities:

  • Animals
  • Food

These entities are very simple, only contain the primary key and a column with the name. The many to many association requires a Join Table where the primary keys of each table will be associated.

With these association each Animal can have many different Food so does each Food can have many different Animals.

Steps to create this Many to Many Association.

Step 1. Open a ssh terminal and go to the root of your Symfony project.

 

Step 2.  Assuming you already have the symfony_db schema with the tables on it:

php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity

 

UPDATE on May-2023: I just found today that this doctrine:mapping:import that was deprecated a couple of years ago, may not longer work. Please proceed with caution and review carefully the generated Entity files.

 

That's it! the new Entities and the association are generated automatically by Symfony.

The Animals Entity will look like:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Animals
 *
 * @ORM\Table(name="animals")
 * @ORM\Entity
 */
class Animals
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="animal_name", type="string", length=45, nullable=false)
     */
    private $animalName;

    /**
     * @var Collection
     *
     * @ORM\ManyToMany(targetEntity="Food", inversedBy="animals")
     * @ORM\JoinTable(name="animals_has_food",
     *   joinColumns={
     *     @ORM\JoinColumn(name="animals_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="food_id", referencedColumnName="id")
     *   }
     * )
     */
    private $food = array();

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->food = new ArrayCollection();
    }

}

And the Food one:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Food
 *
 * @ORM\Table(name="food")
 * @ORM\Entity
 */
class Food
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="food_name", type="string", length=45, nullable=false)
     */
    private $foodName;

    /**
     * @var Collection
     *
     * @ORM\ManyToMany(targetEntity="Animals", mappedBy="food")
     */
    private $animals = array();

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->animals = new ArrayCollection();
    }

}

 

In this case the Metadata for the joining table is kept in the Animals Entity.

From here you can go and generate your Getters and Setters:

$ php bin/console make:entity --regenerate

Now the Animals Entity will look like:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Animals
 *
 * @ORM\Table(name="animals")
 * @ORM\Entity
 */
class Animals
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="animal_name", type="string", length=45, nullable=false)
     */
    private $animalName;

    /**
     * @var Collection
     *
     * @ORM\ManyToMany(targetEntity="Food", inversedBy="animals")
     * @ORM\JoinTable(name="animals_has_food",
     *   joinColumns={
     *     @ORM\JoinColumn(name="animals_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="food_id", referencedColumnName="id")
     *   }
     * )
     */
    private $food = array();

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->food = new ArrayCollection();
    }
    
    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }
    
    /**
     * @return string|null
     */
    public function getAnimalName(): ?string
    {
        return $this->animalName;
    }
    
    /**
     * @param string $animalName
     * @return $this
     */
    public function setAnimalName(string $animalName): self
    {
        $this->animalName = $animalName;

        return $this;
    }

    /**
     * @return Collection<int, Food>
     */
    public function getFood(): Collection
    {
        return $this->food;
    }
    
    /**
     * @param Food $food
     * @return $this
     */
    public function addFood(Food $food): self
    {
        if (!$this->food->contains($food)) {
            $this->food->add($food);
        }

        return $this;
    }
    
    /**
     * @param Food $food
     * @return $this
     */
    public function removeFood(Food $food): self
    {
        $this->food->removeElement($food);

        return $this;
    }

}

 

 

 

 

 

 

 

PHP Fast Page Manager allows to execute PHP Code very fast. The best way to have a high performance site is to pair it with Nginx Web Server.

Prerequisites:

Install PHP7 with FPM in a Vagrant Centos 7

 

Step 1. Tell PHP-FPM that you will be using Nginx as Web Server. open with a text editor this file:

[vagrant@localhost ~]$ sudo nano /etc/php-fpm.d/www.conf

look for user and group and chage them from apache to nginx:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;	will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

save the file.

 

Step 2. Edit the Nginx virtual host conf file that will point to the php project:

[vagrant@localhost ~]$ sudo nano /etc/nginx/conf.d/php_code.conf

make sure you have this:

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include        fastcgi_params;
    }

Save the file.

This code snippet is telling Nginx that all files with php extension must be passed to the Fast Page Manager Server listening in port 9000.

 

Step 3. Check your nginx configuration is correct:

[vagrant@localhost ~]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[vagrant@localhost ~]$ 

If you don't get this response, something is wrong in your Nginx config file and you wont be able to serve web pages.

 

Step 4. Restart services:

[vagrant@localhost ~]$ sudo systemctl restart php-fpm
[vagrant@localhost ~]$ sudo systemctl restart nginx
[vagrant@localhost ~]$ 

 

 

Once you have installed and configured your Vagrant Virtual Machine with Linux Centos 7 follow these simple steps to get up and running PHP7-FPM (Fast Page Manager):

Step 1. Once you vagrant up the VM, vagrant ssh to open a ssh console:

$ vagrant up
$ vagrant ssh
[vagrant@localhost ~]$ 

 

Step 2. Make sure you have epel repository available

[vagrant@localhost ~]$ sudo yum -y update
[vagrant@localhost ~]$ sudo yum -y install epel-release

 

 Step 3. Get Remi Repository:

[vagrant@localhost ~]$ wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[vagrant@localhost ~]$ sudo rpm -Uvh remi-release-7.rpm

 

Step 4. Install PHP-FPM

[vagrant@localhost ~]$ sudo yum install yum-utils -y
[vagrant@localhost ~]$ sudo yum-config-manager --enable remi-php74
[vagrant@localhost ~]$ sudo yum -y --enablerepo=remi,remi-php74 install php-fpm php-common
[vagrant@localhost ~]$ sudo systemctl start php-fpm
[vagrant@localhost ~]$ sudo systemctl enable php-fpm

 

Next Up:

 Nginx with PHP-FPM in Vagrant Centos 7 Virtual Machine

 

 

 

 

 

If you are planning to start a Web Site or need a Compute Instance (Virtual Private Server - VPS) for your project, there is a way to start for free, just follow these simple steps to get up and running a very powerful cloud server capable of running a High Performance Application including lot of space for the Database and asset storage.

At the moment of this writing Oracle Cloud is in my opinion the best option.

Oracle is an old Company and is practically the father of Databases.

Recently, they decided to be part of a multi billion Cloud Service Provider Industry dominated mostly for 3 big players: Amazon, Microsoft and Google.

As part of their marketing strategy and to showcase their infrastructure, they are offering a very nice, plenty of resources pool of Always Free Products.

 

Here is the recipe to get you a very powerful server for running any kind of software applications:

Step 1. Visit Oracle Cloud and sign up for a new Account clicking on Start for free.

Step 2. Once you have you Account set up, sign in and you will see your dashboard.Click on Instances Compute. (If not available in Pinned select Compute->Instances from the top left menu).

 

 Step 3. Click on the Create instance blue button.

 

 Step 4. Click on the Create instance blue button.

Step 5. You have the option to create up to 4 instances of this shape: VM.Standard.A1.Flex or 2 with double resources or 1 with up to 4 virtual cores and up to a massive amount of 24 GB RAM!

Notice that you need to select Oracle Linux and Ampere powered instances to be able to get this amount of resources for free.

 Step 6. Now that the instance is created, you need to attach it to a VNIC (Virtual Network interface).

 

 

 Step 7. If you created only one instance with 50GB of boot volume, then you have available up to 150GB of block volume storage that you may want to create, attach and mount as a directory in your instance.

 

Now you have a fully functional COMPLETELY FREE Super Powerful Linux Server to install and run anything you want.

 

Further reading:

Oracle tutorial on launching a Linux Instance.