escape

RUN..

Hi folks, and let’s continue our runaway in this second part of the Escape the Symfony Form jail series

(Please refer to Part 1 to get into the context.)

So we came to the point of having two Entities Country and Team related to each others with a OneToMany association.

Set-up the User Entity:

Now we have to Create the User Entity. We will use the defacto FOSUserBundle, and I think the documentation is clear enough to show you how to install the bundle and get it up and running.

Next, we’re going to create a ManyToMany association between User & Team, as each User can up-vote many Teams and  each Team can be up-voted by many Users [User (n) <–>(n) Team]

So after adding Doctrine annotations and running repository generators:

# php bin/console doctrine:generate-entites AppBundle:User
# php bin/console doctrine:generate-entites AppBundle:Team

Here are what User and Team classes should look like:

User.php

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        $this->upvotedTeams = new ArrayCollection();
    }

    /**
     * @ORM\ManyToMany(targetEntity="Team",mappedBy="upvoters",cascade={"persist"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $upvotedTeams;
    


    /**
     * Add upvotedTeam
     *
     * @param \AppBundle\Entity\Team $upvotedTeam
     *
     * @return User
     */
    public function addUpvotedTeam(\AppBundle\Entity\Team $upvotedTeam)
    {
        $this->upvotedTeams[] = $upvotedTeam;

        return $this;
    }

    /**
     * Remove upvotedTeam
     *
     * @param \AppBundle\Entity\Team $upvotedTeam
     */
    public function removeUpvotedTeam(\AppBundle\Entity\Team $upvotedTeam)
    {
        $this->upvotedTeams->removeElement($upvotedTeam);
    }

    /**
     * Get upvotedTeams
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUpvotedTeams()
    {
        return $this->upvotedTeams;
    }
}

 Team.php

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        $this->upvotedTeams = new ArrayCollection();
    }

    /**
     * @ORM\ManyToMany(targetEntity="Team",mappedBy="upvoters",cascade={"persist"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $upvotedTeams;
    


    /**
     * Add upvotedTeam
     *
     * @param \AppBundle\Entity\Team $upvotedTeam
     *
     * @return User
     */
    public function addUpvotedTeam(\AppBundle\Entity\Team $upvotedTeam)
    {
        $this->upvotedTeams[] = $upvotedTeam;

        return $this;
    }

    /**
     * Remove upvotedTeam
     *
     * @param \AppBundle\Entity\Team $upvotedTeam
     */
    public function removeUpvotedTeam(\AppBundle\Entity\Team $upvotedTeam)
    {
        $this->upvotedTeams->removeElement($upvotedTeam);
    }

    /**
     * Get upvotedTeams
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUpvotedTeams()
    {
        return $this->upvotedTeams;
    }
}

Now all we have to do is to persist these Entities to the Database

# php bin/console doctrine:schema:update --force
Updating database schema...
Database schema updated successfully! "4" queries were executed

Let’s add some countries and teams to our database. Just run these SQL queries on your favorite administrator:

INSERT INTO `country`(`text`,`id`) VALUES
('Spain',1),
('England',2),
('Germany',3),
('Italy',4),
('France',5),
('Portugal',6),
('Ukraine',7),
('Netherlands',8),
('Turkey',9),
('Argentina',10),
('Czech Republic',11),
('Switzerland',12),
('Russia',13),
('Scotland',14),
('Serbia',15),
('Brazil',16),
('Estonia',17),
('Bulgaria',18);

INSERT INTO team (`text`,`country_id`) VALUES 
('Barcelona',1),
('Manchester City',2),
('Real Madrid',1),
('Bayern München',3),
('Juventus',4),
('Paris Saint Germain',5),
('Atlético Madrid',1),
('Tottenham Hotspur',2),
('Chelsea',2),
('SSC Napoli',4),
('Manchester United',2),
('Liverpool FCRoma',2),
('FC PortoArsenal',4),
('Shakhtar Donetsk',6),
('PSV Eindhoven',2),
('Lyon',7),
('Sporting',8),
('Ajax',5),
('Lazio',6),
('Beşiktaş',8),
('Inter Milan',4),
('Monaco',9),
('Valencia',4),
('Atalanta',5),
('Boca Juniors',1),
('Borussia Dortmund',4),
('Viktoria Plzeň',10),
('Bayer Leverkusen',3),
('Villarreal',11),
('Dynamo KyivSchalke ',3),
('Basel',1),
('Athletic Bilbao',7),
('Benfica',3),
('Zenit St. ',12),
('Petersburg',1),
('RasenBallsport Leipzig',6),
('Olympique Marseille',13),
('Independiente',3),
('Borussia ',5),
('Mönchengladbach',10),
('Celtic',3),
('Crvena Zvezda',14),
('Grêmio',15),
('Hoffenheim',16),
('Levadia ',3),
('Tallinn',17),
('Ludogorets Razgrad',18),
('Sevilla',1),
('San Lorenzo',10),
('Flora Tallinn',17);

Let’s also create two users: medunes & ronaldo. We will use FOSUser CLI for this:

# php bin/console fos:user:create
Please choose a username:medunes
Please choose an email:contact@medunes.net
Please choose a password:choose_a_strong_password

Created user medunes

# php bin/console fos:user:create
Please choose a username:ronaldo
Please choose an email:ronaldo@medunes.net
Please choose a password:choose_a_strong_password

Created user ronaldo

So now, we have two users,  eighteen countries and fifty teams, and each user can up-vote to zero-to-many teams.

Enough of back-end engineering, let’s turn to the front-end realm (We are supposed to be full-stack right?  😉 )

In the next Part, we will set up a login interface and an up-voting view so that a user can choose his favorite teams

Stay connected!