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. Author¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // src/Tutorial/BlogBundle/Entity/Author.php
namespace Tutorial\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Embeddable
*/
class Author
{
/**
* @ORM\Column(type = "string")
*/
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
|
2.1.2. Post¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | // 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")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank()
* @Assert\Length(min="10", max=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $abstract;
/**
* @ORM\Column(type="text")
*
* @Assert\NotBlank()
*/
private $content;
/**
* @ORM\Column(type="boolean")
*/
private $enabled;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $updated_at;
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
*/
private $comments;
/**
* @ORM\ManyToMany(targetEntity="Tag")
*/
private $tags;
/**
* @ORM\Embedded(class="Author")
*/
private $author;
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
$this->created_at = new \DateTime("now");
$this->author = new Author('admin');
}
public function __toString()
{
return $this->getTitle();
}
public function getAuthor()
{
return $this->author;
}
}
|
2.1.3. Tag¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // 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")
*/
private $id;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank()
*/
private $name;
/**
* @ORM\Column(type="boolean")
*/
private $enabled;
/**
* @ORM\ManyToMany(targetEntity="Post")
*/
private $posts;
public function __construct()
{
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString()
{
return $this->getName();
}
}
|
2.1.4. Comment¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // 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")
*/
private $id;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank()
*/
private $name;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank()
*/
private $email;
/**
* @ORM\Column(type="string")
*/
private $url;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
private $message;
/**
* @ORM\ManyToOne(targetEntity="Post")
*/
private $post;
public function __toString()
{
return $this->getName();
}
}
|
Note
For advanced usage, $id
might be implemented as an object. The bundle will automatically resolve its string
representation from the ID object using $entity->getId()->__toString()
(if implemented) when needed
(e.g., for generating url / rendering).
For example, in a use case where InnoDB-optimised binary UUIDs is implemented:
class Comment
{
/**
* @var \Ramsey\Uuid\UuidInterface
* @Id
* @Column(type="uuid_binary_ordered_time", unique=true)
* @GeneratedValue(strategy="CUSTOM")
* @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
*/
private $id;
// ...
}
As $comment->getId()
returns an object of \Ramsey\Uuid\UuidInterface
and the bundle recognizes
that it has offered a __toString
method, $comment->getId()->__toString()
is called to resolve
the ID string value as part of the entity url generation.
2.2. Generate getters and setters¶
Fill the entities with getters and setters by running the following command:
1 | bin/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:
1 | bin/console doctrine:schema:update --force
|