Laravel Doctrine

Laravel8.x 安装 laravel-doctrine/orm

安装

1
2
3
4
5
6
# 降低相关包版本,以便安装 laravel-doctrine/orm
composer require doctrine/dbal:"^2.6"
composer require doctrine/inflector:"^1.4"
composer require laravel-doctrine/orm:"~1.7"
# 发布配置文件
php artisan vendor:publish --tag="config" --provider="LaravelDoctrine\ORM\DoctrineServiceProvider"

生成元数据和实休

1
2
3
4
5
6
7
8
9
10
# 生成元数据
php artisan doctrine:mapping:import --namespace=App\\Entities\\ xml database/mappings
# 配置 doctrine.php
'meta' => env('DOCTRINE_METADATA', 'xml'),
'paths' => [base_path('database/mappings')],
# 生成实体
php artisan doctrine:generate:entities / --generate-methods

# 需注意一点,如果 meta => env('DOCTRINE_METADATA', 'annotations'), 则生成实体时,命名必须包含 --generate-annotations
php artisan doctrine:generate:entities / --generate-methods --generate-annotations

DEMO

Repository.php

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
<?php

namespace App\Repositories;

use ReflectionClass;
use Doctrine\ORM\{EntityRepository, OptimisticLockException, ORMException};

/**
* Class DoctrineBaseRepository
*
* @package App\Repository
*/
class Repository extends EntityRepository
{
/**
* @param $data
*
* @return object
*/
public function create($data): object
{
$entity = new $this->_entityName();
return $this->prepare($entity, $data);
}

/**
* @param $data
* @param $id
*
* @return object
*/
public function update($data, $id): object
{
$entity = $this->find($id);
return $this->prepare($entity, $data);
}

/**
* @param $object
*
* @return object
* @throws ORMException|OptimisticLockException
*/
public function save($object): object
{
$this->_em->persist($object);
$this->_em->flush($object);

return $object;
}

/**
* @param $object
*
* @return object
* @throws ORMException|OptimisticLockException
*/
public function delete($object): object
{
$this->_em->remove($object);
$this->_em->flush($object);

return $object;
}

/**
* @param object $entity
* @param $data
*
* @return object
*/
protected function prepare(object $entity, $data): object
{
$set = 'set';
$ref = new ReflectionClass($entity);
foreach ($ref->getProperties() as $reflectionProperty) {
$field = $reflectionProperty->getName();
if (isset($data[$field])) {
$setter = $set . ucfirst($field);
$entity->$setter($data[$field]);
}
}

return $entity;
}
}

UserRepository.php

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

namespace App\Repositories;

use App\Entities\Users;
use Doctrine\ORM\EntityManager;

class UserRepository extends Repository
{
protected $_entityName = Users::class;

public function __construct(EntityManager $em)
{
parent::__construct($em, $em->getClassMetadata($this->_entityName));
}
}

UserController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace App\Http\Controllers;

class UserController
{
/**
* @param string $repositoryName
*
* @return Repository
*/
public function getRepository(string $repositoryName)
{
return app($repositoryName);
}

public function index()
{
$repository = $this->getRepository(UserRepository::class);
dd($repository->find(1));
}
}
0%