ORM- Entity Class - Schema Generate

7/14/2021

# Description:

Entities Class sẽ mapping với dữ liệu database và có thể genarate database schema từ các Entity Class này. Document: https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/basic-mapping.html

# Example:

Như config ở Entity Manager, các Entity được khai báo tại src/Entities

Note: Doctrine sẽ đọc các comment để gerenate schema

User:

<?php

namespace Nin\Entities;

use Doctrine\ORM\Mapping as ORM;

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

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

    /**
     * Many User have Many Posts.
     * @ORM\ManyToMany(targetEntity="Post")
     * @ORM\JoinTable(name="user_post",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id", unique=true)}
     *      )
     */
    private $posts;

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

    public function getId()
    {
        return $this->id;
    }

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

    public function setName($name)
    {
        $this->name = $name;
    }
}

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

Post:

<?php

namespace Nin\Entities;

use Doctrine\ORM\Mapping as ORM;

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

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

    public function getId()
    {
        return $this->id;
    }

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

    public function setTitle($title)
    {
        $this->title = $title;
    }
}

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

# Generating the Database Schema:

Dùng Doctrine Schema Tool để genarate, tương tự như Laravel artisan migrate

# File config cli:

config/cli-config.php

<?php

use Nin\Libs\ORM\ORMManagerContract;

$app = require_once __DIR__ . "/../bootstrap/app.php";

$orm = $app->make(ORMManagerContract::class);

function getEntityManager(ORMManagerContract $orm)
{
    return $orm->getEntityManager();
}

$entityManager = getEntityManager($orm);

return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Doctrine Entity Manager đã được binding trong DI Container nên chỉ cần resolve từ app.

# Create
$ vendor/bin/doctrine orm:schema-tool:create

# Drop
$ vendor/bin/doctrine orm:schema-tool:drop --force

# Update
$ vendor/bin/doctrine orm:schema-tool:update --force
1
2
3
4
5
6
7
8

Run create

 Creating database schema...


 [OK] Database schema created successfully!
1
2
3
4

Trong database đã có 3 table:

users
posts
user_post
1
2
3

Packagist: https://packagist.org/packages/nin/nin (opens new window)