安装
2.11.0 安装
Installation and Configuration - Doctrine Object Relational Mapper (ORM)
composer.json
composer install
{
"require": {
"symfony/cache": "^5.4",
"doctrine/annotations": "^1.13",
"doctrine/orm": "^2.11.0",
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://mirrors.cloud.tencent.com/composer/"
}
}
}
控制台配置文件
<?php
/**
* 配置 Doctrine 控制台
*/
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
$paths = ['/xxx/entity/sys'];
$isDevMode = true; //开发模式,非开发模式会有ddl缓存
$dbParams = [
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'xxxxxx',
'dbname' => 'testdb',
'port' => 3306,
'charset' => 'utf8mb4'
];
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
return ConsoleRunner::createHelperSet($entityManager);
创建单元文件
Basic Mapping - Doctrine Object Relational Mapper (ORM)
CLI生成DDL同步数据库 toDatabase.bat
E:\www\project\qgmvc\v1.0\composer\vendor\bin\doctrine orm:schema-tool:update --force --dump-sql
pause
CLI生成getXxxx(), setXxxx(), getSet.bat
E:\www\project\qgmvc\v1.0\composer\vendor\bin\doctrine orm:generate:entities E:\www\project\clientSite\entity\sys
pause
代理类
<?php
/**
* orm
* @version 1.0.0
[doctrine]
http://www.symfonychina.com/doc/current/doctrine.html
https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html
*/
namespace lib;
use \Doctrine\ORM\Tools\Setup;
use \Doctrine\ORM\EntityManager;
class orm
{
private $manager = null;
private $dbkey = null;
protected $transactionCounter = 0;
public function __construct(string $dbkey)
{
$isDevMode = true;
$dbParams = \mvc::$cfg['net']['db'][$dbkey];
$config = Setup::createAnnotationMetadataConfiguration($dbParams['entity'], $isDevMode);
$this->manager = EntityManager::create($dbParams, $config);
$this->dbkey = $dbkey;
}
public function getManager()
{
return $this->manager;
}
public function begin()
{
if($this->transactionCounter == 0){
$this->manager->getConnection()->beginTransaction();
}
$this->transactionCounter++;
}
public function rollBack()
{
$this->manager->getConnection()->rollBack();
}
public function commit()
{
$this->transactionCounter--;
if($this->transactionCounter==0){
try{
$this->manager->getConnection()->commit();
}catch(\Exception $e){
$this->manager->getConnection()->rollBack();
throw $e;
}
}
}
/**
* 持久化数据准备
*/
public function persist($entity)
{
$this->manager->persist($entity);
}
/**
* 提交到数据库
*/
public function flush()
{
try{
$this->manager->flush();
}catch(\Exception $e){
throw $e;
}
}
/**
* 对应元数据仓库
* store()->createQueryBuilder('e')->select('e')->getQuery()->getArrayResult();
* store()->fetchAll(ORM_ARRAY);
*/
public function store(string $entityName)
{
return D('lib.repository',$entityName,$this);
}
/**
* 执行原生SQL
* @return $st fetch, fetchAll
*/
public function query(string $sql)
{
try{
\mvc::$lastsql = $sql;
return $this->manager->getConnection()->query($sql);
}catch(\Exception $e){
throw $e;
}
}
public function __call($method,$params)
{
return call_user_func_array([$this->manager,$method],$params);
}
public function __sleep()
{
return ['dbkey'];
}
}
/**
* repository
* @version 1.0.0
*/
class repository{
protected $orm = null;
protected $entityName = 0;
protected $doctrineRepository = null;
public function __construct(string $entityName, orm $orm)
{
$entityName = '\\'.ltrim(str_replace('.','\\',$entityName),'\\');
if(substr($entityName,1,6)!='entity'){
$entityName = '\\entity'.$entityName;
}
$this->entityName = $entityName;
$this->orm = $orm;
$this->doctrineRepository = $orm->getManager()->getRepository($entityName);
}
public function link($tag){
return D('lib.queryBuilder',$this->entityName,$tag,$this->orm,$this);
}
public function toArray($items){
$result = [];
$isOne = false;
if(!is_array($items)){
$items = [$items];
$isOne = true;
}
$class = new \ReflectionClass($this->entityName);
$properties = $class->getDefaultProperties();
foreach($items as $item){
$arr = [];
foreach($properties as $propertie=>$null){
$value = call_user_func([$item,'get'.ucfirst($propertie)]);
switch(gettype($value)){
case 'object':
switch(get_class($value)){
case 'DateTime':
$value = $value->format(\mvc::$cfg['datetimeformat']);
break;
}
break;
}
$arr[$propertie] = $value;
}
array_push($result,$arr);
}
return $isOne ? $result[0] :$result;
}
public function __call($method,$params)
{
$isToArray = false;
if(substr($method,0,4)=='find' && isset($params[0]) && $params[0]==ORM_ARRAY){
$isToArray = true;
}
if($isToArray){
$result = call_user_func_array([$this->doctrineRepository,$method],$params);
return $this->toArray($result);
}else{
return call_user_func_array([$this->doctrineRepository,$method],$params);
}
}
public function __sleep()
{
return ['orm','entityName'];
}
}
/**
* queryBuilder
* @version 1.0.0
*/
class queryBuilder{
protected $orm = null;
protected $repository = null;
protected $entityName = '';
protected $tag = '';
protected $queryBuilder = null;
public function __construct(string $entityName, string $tag, orm $orm, repository $repository)
{
$this->orm = $orm;
$this->repository = $repository;
$this->entityName = $entityName;
$this->tag = $tag;
$this->queryBuilder = $orm->getManager()->getRepository($entityName)->createQueryBuilder($tag);
}
public function getQuery()
{
return D('lib.query',$this->entityName,$this->tag,$this->orm, $this->repository, $this);
}
public function getQueryFromParent()
{
return $this->queryBuilder->getQuery();
}
public function select(...$params)
{
call_user_func_array([$this->queryBuilder,'select'],$params);
return $this;
}
public function __call($method,$params)
{
return call_user_func_array([$this->queryBuilder,$method],$params);
}
public function __sleep()
{
return ['orm','entityName','tag'];
}
}
/**
* query
* @version 1.0.0
*/
class query{
protected $orm = null;
protected $repository = null;
protected $queryBuilder = null;
protected $entityName = '';
protected $tag = '';
protected $query = null;
public function __construct(string $entityName, string $tag, orm $orm, repository $repository, queryBuilder $queryBuilder)
{
$this->entityName = $entityName;
$this->tag = $tag;
$this->orm = $orm;
$this->repository = $repository;
$this->queryBuilder = $queryBuilder;
$this->query = call_user_func([$queryBuilder,'getQueryFromParent']);
}
public function toArray(...$params)
{
$result = call_user_func_array([$this->query,'getResult'],$params);
return $this->repository->toArray($result);
}
public function __call($method,$params)
{
return call_user_func_array([$this->query,$method],$params);
}
public function __sleep()
{
return ['orm','entityName','tag','repository'];
}
}
调用测试
$orm = orm();
$user = $orm->store('sys.user');
$list = $user->link('e')->select('e')->getQuery()->toArray();
return success($list);
//$list = $user->findAll(ORM_ARRAY);
//$list = $user->toArray($list);
foreach($list as $item){
print_r($item);
}
$test = C('action.user');
dd($test->test2());
exit();
$orm = orm();
$user = C('model.core.user',$orm);
$list = $user->findAll();
dd(json_encode($list,true));
//$sql = 'SELECT * FROM sys_user';
//$result = $orm->query($sql);
//dd($result->fetch());
$user = M('core.user',$orm);
$user->find('test1648719565');
$user->password = '7';
$orm->persist($user);
$orm->flush();
dd($user->ctime);
exit();