ORM- Repository

7/14/2021

# Description:

Tạo lớp repository để thực hiện thao tác truy xuất với DB.

# Repository:

Contract

<?php

namespace Nin\Libs\Repository;

interface RepositoryContract
{
    /**
     * Retrieve all data of repository
     */
    public function all();

    /**
     * Save data of repository
     *
     * @param $entity
     * @return bool
     */
    public function save($entity);
    
    /*
    *
    * More method
    */

}
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

Base repository

<?php

namespace Nin\Libs\Repository;

use Nin\Libs\Container\ApplicationContract;
use Nin\Libs\ORM\ORMManagerContract;

abstract class AbstractRepository implements RepositoryContract
{
    protected ApplicationContract $app;
    protected $entityManager;

    public function __construct(ApplicationContract $app)
    {
        $this->app = $app;
        $this->entityManager = $this->getEntityManager($app->make(ORMManagerContract::class));
    }

    abstract public function entity();

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

    public function getEntityInstance()
    {
        $entity = $this->entity();
        return new $entity;
    }

    public function getRepository()
    {
        return $this->entityManager
            ->getRepository($this->entity());
    }

    public function all()
    {
        $repository = $this->getRepository();
        return $repository->findAll();
    }

    public function save($entity): bool
    {
        try {
            $entityManager = $this->entityManager;
            $entityManager->persist($entity);
            $entityManager->flush();
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }
}

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

# Usage:

Make UserRepository:

<?php

namespace Nin\Repositories;

use Nin\Entities\User;
use Nin\Libs\Repository\AbstractRepository;

class UserRepository extends AbstractRepository
{
    public function entity()
    {
        return User::class;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Controller:

<?php

namespace Nin\Controllers;

use Nin\Entities\User;
use Nin\Repositories\UserRepository;

class FooController
{
    protected UserRepository $userRepository;

    public function __construct(
        UserRepository $userRepository
    ) {
        $this->userRepository = $userRepository;
    }

    public function indexAction()
    {
        vd($this->userRepository->all());
    }

    public function createAction()
    {
        $entity = $this
            ->setEntityProperty($this->userRepository
                ->getEntityInstance());
        $this->userRepository->save($entity);
        vd(this->userRepository->all()));
    }

    protected function setEntityProperty(User $user)
    {
        $user->setName('user3');
        return $user;
    }
}

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

Ouput

// indexAction

^ array:3 [0 => Nin\Entities\User {#164 ▼
    #id: 1
    #name: "admin"
    -posts: Doctrine\ORM\PersistentCollection {#179 ▶}
  }
  1 => Nin\Entities\User {#181 ▼
    #id: 2
    #name: "user"
    -posts: Doctrine\ORM\PersistentCollection {#182 ▶}
  }
  2 => Nin\Entities\User {#184 ▼
    #id: 3
    #name: "user2"
    -posts: Doctrine\ORM\PersistentCollection {#185 ▶}
  }
]
    
// createAction
 ^ array:4 [0 => Nin\Entities\User {#180 ▼
    #id: 1
    #name: "admin"
    -posts: Doctrine\ORM\PersistentCollection {#182 ▶}
  }
  1 => Nin\Entities\User {#184 ▼
    #id: 2
    #name: "user"
    -posts: Doctrine\ORM\PersistentCollection {#185 ▶}
  }
  2 => Nin\Entities\User {#187 ▼
    #id: 3
    #name: "user2"
    -posts: Doctrine\ORM\PersistentCollection {#188 ▶}
  }
  3 => Nin\Entities\User {#60 ▼
    #id: 4
    #name: "user3"
    -posts: Doctrine\ORM\PersistentCollection {#148 ▶}
  }
]
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

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