Version

Documentation » Doctrine2 ORM Admin

3. Defining the routing »

« 1. Introduction

2. Defining Entities

This tutorial uses the more verbose xml format of defining entities, but any metadata driver will work fine. The AdminBundle simply interacts with the entities as provided by Doctrine.

2.1. Model definition

Now we need to create the entities that will be used in the blog:

2.1.1. Post

<?php

// src/Tutorial/BlogBundle/Entity/Post.php
namespace Tutorial\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Post
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank()
     * @Assert\Length(min="10", max=255)
     */
    protected $title;

    /**
     * @ORM\Column(type="text")
     */
    protected $abstract;

    /**
     * @ORM\Column(type="text")
     *
     * @Assert\NotBlank()
     */
    protected $content;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $enabled;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created_at;

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
     */
    protected $comments;

    /**
     * @ORM\ManyToMany(targetEntity="Tag")
     */
    protected $tags;

    public function __construct()
    {
        $this->tags     = new \Doctrine\Common\Collections\ArrayCollection();
        $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
        $this->created_at = new \DateTime("now");
    }

    public function __toString()
    {
        return $this->getTitle();
    }
}

2.1.2. Tag

<?php

// src/Tutorial/BlogBundle/Entity/Tag.php
namespace Tutorial\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Tag
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    protected $name;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $enabled;

    /**
     * @ORM\ManyToMany(targetEntity="Post")
     */
    protected $posts;

    public function __construct()
    {
        $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function __toString()
    {
        return $this->getName();
    }
}

2.1.3. Comment

<?php

// src/Tutorial/BlogBundle/Entity/Comment.php
namespace Tutorial\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Comment
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     *
     * @Assert\NotBlank()
     */
    protected $name;


    /**
     * @ORM\Column(type="string")
     *
     * @Assert\NotBlank()
     */
    protected $email;

    /**
     * @ORM\Column(type="string")
     */
    protected $url;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    protected $message;

    /**
     * @ORM\ManyToOne(targetEntity="Post")
     */
    protected $post;

    public function __toString()
    {
        return $this->getName();
    }
}

2.2. Generate getters and setters

Fill the entities with getters and setters by running the following command:

php app/console doctrine:generate:entities Tutorial

2.3. Creating the Database

Create the database related to the entities and the mapping by running the following command:

php app/console doctrine:schema:update --force

Found a typo? Something is wrong in this documentation? Just fork and edit it!