[ Part 3 ]- Data Model - Creating entities

Symfony 3 Jobeet tutorial - We are creatings database and model for our Jobeet application

Posted by Lukasz D. Tulikowski on February 4, 2017

 

Table of content


    The Relational Model

    The user stories from the previous day describe the main objects of our project: jobs, affiliates, and categories. Here is the corresponding entity relationship diagram:

    symfony Jobeet diagram

    Once you have your connection set up and database created, you can create MySQL schema with doctrine command. But how to define all tables and files that should be created? Let’s use doctrines Entities to build a model, upon which we generate our MySQL schema.

    You can write your Entities by yourself. It is not difficult. Will use PHP Annotations not a YAML or XML mapping.

    If you like to generate Entities from YAML file and still use annotation, this article can be helpful.

    Creating an Entities Classes

    The class - often called an “entity”, meaning a basic class that holds data - is simple and helps fulfil the business requirement of needing products in your application. This class can’t be persisted to a database yet - it’s just a simple PHP class.

    We will create an entity for each table shown in the diagram.

    First, create folder Entity in your App/src/AppBundle directory. Then create entities.

    Category

    <?php
    // src/AppBundle/Entity/Category.php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * Category
     *
     * @ORM\Table(name="category")
     * @ORM\Entity
     */
    class Category
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=255, unique=true)
         */
        private $name;
    
        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\Job", mappedBy="category")
         */
        private $jobs;
    
        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\CategoryAffiliate", mappedBy="category")
         */
        private $category_affiliates;
    
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
            $this->category_affiliates = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set name
         *
         * @param string $name
         *
         * @return Category
         */
        public function setName($name)
        {
            $this->name = $name;
    
            return $this;
        }
    
        /**
         * Get name
         *
         * @return string
         */
        public function getName()
        {
            return $this->name;
        }
    
        /**
         * Add job
         *
         * @param \AppBundle\Entity\Job $job
         *
         * @return Category
         */
        public function addJob(\AppBundle\Entity\Job $job)
        {
            $this->jobs[] = $job;
    
            return $this;
        }
    
        /**
         * Remove job
         *
         * @param \AppBundle\Entity\Job $job
         */
        public function removeJob(\AppBundle\Entity\Job $job)
        {
            $this->jobs->removeElement($job);
        }
    
        /**
         * Get jobs
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getJobs()
        {
            return $this->jobs;
        }
    
        /**
         * Add categoryAffiliate
         *
         * @param \AppBundle\Entity\CategoryAffiliate $categoryAffiliate
         *
         * @return Category
         */
        public function addCategoryAffiliate(\AppBundle\Entity\CategoryAffiliate $categoryAffiliate)
        {
            $this->category_affiliates[] = $categoryAffiliate;
    
            return $this;
        }
    
        /**
         * Remove categoryAffiliate
         *
         * @param \AppBundle\Entity\CategoryAffiliate $categoryAffiliate
         */
        public function removeCategoryAffiliate(\AppBundle\Entity\CategoryAffiliate $categoryAffiliate)
        {
            $this->category_affiliates->removeElement($categoryAffiliate);
        }
    
        /**
         * Get categoryAffiliates
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getCategoryAffiliates()
        {
            return $this->category_affiliates;
        }
    }
    

    Job

    <?php
    // src/AppBundle/Entity/Job.php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * Job
     *
     * @ORM\Table(name="job")
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class Job
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="type", type="string", length=255, nullable=true)
         */
        private $type;
    
        /**
         * @var string
         *
         * @ORM\Column(name="company", type="string", length=255)
         */
        private $company;
    
        /**
         * @var string
         *
         * @ORM\Column(name="logo", type="string", length=255, nullable=true)
         */
        private $logo;
    
        /**
         * @var string
         *
         * @ORM\Column(name="url", type="string", length=255, nullable=true)
         */
        private $url;
    
        /**
         * @var string
         *
         * @ORM\Column(name="position", type="string", length=255)
         */
        private $position;
    
        /**
         * @var string
         *
         * @ORM\Column(name="location", type="string", length=255)
         */
        private $location;
    
        /**
         * @var string
         *
         * @ORM\Column(name="description", type="text")
         */
        private $description;
    
        /**
         * @var string
         *
         * @ORM\Column(name="how_to_apply", type="text")
         */
        private $how_to_apply;
    
        /**
         * @var string
         *
         * @ORM\Column(name="token", type="string", length=255, unique=true)
         */
        private $token;
    
        /**
         * @var boolean
         *
         * @ORM\Column(name="is_public", type="boolean", nullable=true)
         */
        private $is_public;
    
        /**
         * @var boolean
         *
         * @ORM\Column(name="is_activated", type="boolean", nullable=true)
         */
        private $is_activated;
    
        /**
         * @var string
         *
         * @ORM\Column(name="email", type="string", length=255)
         */
        private $email;
    
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="expires_at", type="datetime")
         */
        private $expires_at;
    
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="created_at", type="datetime")
         */
        private $created_at;
    
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="updated_at", type="datetime", nullable=true)
         */
        private $updated_at;
    
        /**
         * @var \AppBundle\Entity\Category
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="jobs")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="category_id", referencedColumnName="id")
         * })
         */
        private $category;
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set type
         *
         * @param string $type
         *
         * @return Job
         */
        public function setType($type)
        {
            $this->type = $type;
    
            return $this;
        }
    
        /**
         * Get type
         *
         * @return string
         */
        public function getType()
        {
            return $this->type;
        }
    
        /**
         * Set company
         *
         * @param string $company
         *
         * @return Job
         */
        public function setCompany($company)
        {
            $this->company = $company;
    
            return $this;
        }
    
        /**
         * Get company
         *
         * @return string
         */
        public function getCompany()
        {
            return $this->company;
        }
    
        /**
         * Set logo
         *
         * @param string $logo
         *
         * @return Job
         */
        public function setLogo($logo)
        {
            $this->logo = $logo;
    
            return $this;
        }
    
        /**
         * Get logo
         *
         * @return string
         */
        public function getLogo()
        {
            return $this->logo;
        }
    
        /**
         * Set url
         *
         * @param string $url
         *
         * @return Job
         */
        public function setUrl($url)
        {
            $this->url = $url;
    
            return $this;
        }
    
        /**
         * Get url
         *
         * @return string
         */
        public function getUrl()
        {
            return $this->url;
        }
    
        /**
         * Set position
         *
         * @param string $position
         *
         * @return Job
         */
        public function setPosition($position)
        {
            $this->position = $position;
    
            return $this;
        }
    
        /**
         * Get position
         *
         * @return string
         */
        public function getPosition()
        {
            return $this->position;
        }
    
        /**
         * Set location
         *
         * @param string $location
         *
         * @return Job
         */
        public function setLocation($location)
        {
            $this->location = $location;
    
            return $this;
        }
    
        /**
         * Get location
         *
         * @return string
         */
        public function getLocation()
        {
            return $this->location;
        }
    
        /**
         * Set description
         *
         * @param string $description
         *
         * @return Job
         */
        public function setDescription($description)
        {
            $this->description = $description;
    
            return $this;
        }
    
        /**
         * Get description
         *
         * @return string
         */
        public function getDescription()
        {
            return $this->description;
        }
    
        /**
         * Set howToApply
         *
         * @param string $howToApply
         *
         * @return Job
         */
        public function setHowToApply($howToApply)
        {
            $this->how_to_apply = $howToApply;
    
            return $this;
        }
    
        /**
         * Get howToApply
         *
         * @return string
         */
        public function getHowToApply()
        {
            return $this->how_to_apply;
        }
    
        /**
         * Set token
         *
         * @param string $token
         *
         * @return Job
         */
        public function setToken($token)
        {
            $this->token = $token;
    
            return $this;
        }
    
        /**
         * Get token
         *
         * @return string
         */
        public function getToken()
        {
            return $this->token;
        }
    
        /**
         * Set isPublic
         *
         * @param boolean $isPublic
         *
         * @return Job
         */
        public function setIsPublic($isPublic)
        {
            $this->is_public = $isPublic;
    
            return $this;
        }
    
        /**
         * Get isPublic
         *
         * @return boolean
         */
        public function getIsPublic()
        {
            return $this->is_public;
        }
    
        /**
         * Set isActivated
         *
         * @param boolean $isActivated
         *
         * @return Job
         */
        public function setIsActivated($isActivated)
        {
            $this->is_activated = $isActivated;
    
            return $this;
        }
    
        /**
         * Get isActivated
         *
         * @return boolean
         */
        public function getIsActivated()
        {
            return $this->is_activated;
        }
    
        /**
         * Set email
         *
         * @param string $email
         *
         * @return Job
         */
        public function setEmail($email)
        {
            $this->email = $email;
    
            return $this;
        }
    
        /**
         * Get email
         *
         * @return string
         */
        public function getEmail()
        {
            return $this->email;
        }
    
        /**
         * Set expiresAt
         *
         * @param \DateTime $expiresAt
         *
         * @return Job
         */
        public function setExpiresAt($expiresAt)
        {
            $this->expires_at = $expiresAt;
    
            return $this;
        }
    
        /**
         * Get expiresAt
         *
         * @return \DateTime
         */
        public function getExpiresAt()
        {
            return $this->expires_at;
        }
    
        /**
         * Set createdAt
         *
         * @param \DateTime $createdAt
         *
         * @return Job
         */
        public function setCreatedAt($createdAt)
        {
            $this->created_at = $createdAt;
    
            return $this;
        }
    
        /**
         * Get createdAt
         *
         * @return \DateTime
         */
        public function getCreatedAt()
        {
            return $this->created_at;
        }
    
        /**
         * Set updatedAt
         *
         * @param \DateTime $updatedAt
         *
         * @return Job
         */
        public function setUpdatedAt($updatedAt)
        {
            $this->updated_at = $updatedAt;
    
            return $this;
        }
    
        /**
         * Get updatedAt
         *
         * @return \DateTime
         */
        public function getUpdatedAt()
        {
            return $this->updated_at;
        }
    
        /**
         * Set category
         *
         * @param \AppBundle\Entity\Category $category
         *
         * @return Job
         */
        public function setCategory(\AppBundle\Entity\Category $category = null)
        {
            $this->category = $category;
    
            return $this;
        }
    
        /**
         * Get category
         *
         * @return \AppBundle\Entity\Category
         */
        public function getCategory()
        {
            return $this->category;
        }
        /**
         * @ORM\PrePersist
         */
        public function setCreatedAtValue()
        {
            // Add your code here
        }
    
        /**
         * @ORM\PreUpdate
         */
        public function setUpdatedAtValue()
        {
            // Add your code here
        }
    }
    

    Affiliate

    <?php
    // src/AppBundle/Entity/Affiliate.php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * Affiliate
     *
     * @ORM\Table(name="affiliate")
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class Affiliate
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="url", type="string", length=255)
         */
        private $url;
    
        /**
         * @var string
         *
         * @ORM\Column(name="email", type="string", length=255, unique=true)
         */
        private $email;
    
        /**
         * @var string
         *
         * @ORM\Column(name="token", type="string", length=255)
         */
        private $token;
    
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="created_at", type="datetime")
         */
        private $created_at;
    
        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\CategoryAffiliate", mappedBy="affiliate")
         */
        private $category_affiliates;
    
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->category_affiliates = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set url
         *
         * @param string $url
         *
         * @return Affiliate
         */
        public function setUrl($url)
        {
            $this->url = $url;
    
            return $this;
        }
    
        /**
         * Get url
         *
         * @return string
         */
        public function getUrl()
        {
            return $this->url;
        }
    
        /**
         * Set email
         *
         * @param string $email
         *
         * @return Affiliate
         */
        public function setEmail($email)
        {
            $this->email = $email;
    
            return $this;
        }
    
        /**
         * Get email
         *
         * @return string
         */
        public function getEmail()
        {
            return $this->email;
        }
    
        /**
         * Set token
         *
         * @param string $token
         *
         * @return Affiliate
         */
        public function setToken($token)
        {
            $this->token = $token;
    
            return $this;
        }
    
        /**
         * Get token
         *
         * @return string
         */
        public function getToken()
        {
            return $this->token;
        }
    
        /**
         * Set createdAt
         *
         * @param \DateTime $createdAt
         *
         * @return Affiliate
         */
        public function setCreatedAt($createdAt)
        {
            $this->created_at = $createdAt;
    
            return $this;
        }
    
        /**
         * Get createdAt
         *
         * @return \DateTime
         */
        public function getCreatedAt()
        {
            return $this->created_at;
        }
    
        /**
         * Add categoryAffiliate
         *
         * @param \AppBundle\Entity\CategoryAffiliate $categoryAffiliate
         *
         * @return Affiliate
         */
        public function addCategoryAffiliate(\AppBundle\Entity\CategoryAffiliate $categoryAffiliate)
        {
            $this->category_affiliates[] = $categoryAffiliate;
    
            return $this;
        }
    
        /**
         * Remove categoryAffiliate
         *
         * @param \AppBundle\Entity\CategoryAffiliate $categoryAffiliate
         */
        public function removeCategoryAffiliate(\AppBundle\Entity\CategoryAffiliate $categoryAffiliate)
        {
            $this->category_affiliates->removeElement($categoryAffiliate);
        }
    
        /**
         * Get categoryAffiliates
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getCategoryAffiliates()
        {
            return $this->category_affiliates;
        }
        /**
         * @ORM\PrePersist
         */
        public function setCreatedAtValue()
        {
            $this->created_at = new \DateTime();
        }
    }
    

    CategoryAffiliate

    <?php
    // src/AppBundle/Entity/CategoryAffiliate.php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * CategoryAffiliate
     *
     * @ORM\Table(name="category_affiliate")
     * @ORM\Entity
     */
    class CategoryAffiliate
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var \AppBundle\Entity\Category
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="category_affiliates")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="category_id", referencedColumnName="id")
         * })
         */
        private $category;
    
        /**
         * @var \AppBundle\Entity\Affiliate
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Affiliate", inversedBy="category_affiliates")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="affiliate_id", referencedColumnName="id")
         * })
         */
        private $affiliate;
    
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set category
         *
         * @param \AppBundle\Entity\Category $category
         *
         * @return CategoryAffiliate
         */
        public function setCategory(\AppBundle\Entity\Category $category = null)
        {
            $this->category = $category;
    
            return $this;
        }
    
        /**
         * Get category
         *
         * @return \AppBundle\Entity\Category
         */
        public function getCategory()
        {
            return $this->category;
        }
    
        /**
         * Set affiliate
         *
         * @param \AppBundle\Entity\Affiliate $affiliate
         *
         * @return CategoryAffiliate
         */
        public function setAffiliate(\AppBundle\Entity\Affiliate $affiliate = null)
        {
            $this->affiliate = $affiliate;
    
            return $this;
        }
    
        /**
         * Get affiliate
         *
         * @return \AppBundle\Entity\Affiliate
         */
        public function getAffiliate()
        {
            return $this->affiliate;
        }
    }
    

    Generete database schema

    Once you’ve created entities, you can create database schema

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

    Now in your database, you will find tables and relations, you defined while creating entities. But tables are empty. Let’s put there some example categories and jobs now!