v2.0 2022-01-16 13:15 UTC

This package is auto-updated.

Last update: 2024-09-16 19:18:46 UTC


README

安装

composer require codememory/orm

安装后必须执行以下命令

  • 如果不存在,则创建全局配置
    • php vendor/bin/gc-cdm g-config:init
  • 合并所有配置
    • php vendr/bin/gc-cdm g-config:merge --all

配置概览

# configs/database.yaml

database:
  orm:
    pathWithEntities: 'App/Entities'  # Path with entities
    entityNamespace: 'App\Entities'  # Entity Namespace
    entitySuffix: 'Entity'            # Suffix for class|file
    
    # The same as with entities, only with repositories
    pathWithRepositories: 'App/Repositories'
    repositoryNamespace: 'App\Repositories'
    repositorySuffix: 'Repository'

初始化示例

<?php

use Codememory\Components\Database\Orm\EntityManager;

require_once 'vendor/autoload.php';

// Documentation by database connection https://github.com/codememory1/database-connection
$entityManager = new EntityManager($connector);

EntityManager 方法

  • commit(): EntityManagerInteface 将实体添加到提交

    • object $entity
  • flush(): EntityManagerInterface 将所有提交中的记录添加到表中

  • getRepository(): EntityRepositoryInterface 获取实体存储库

    • string|object $entity
  • isEntity(): bool 检查对象或命名空间是否为实体

    • string|object $entity
  • isExistEntityRepository(): bool 检查是否存在实体的存储库

    • string|object $entity
  • getEntityData(): EntityDataInterface 获取实体数据对象

    • string|object $entity

实体概览

<?php

namespace App\Entities;

use Codememory\Components\Database\Orm\Constructions as ORM;
use App\Repositories\UserRepository;

#[ORM\Entity(tableName: 'users')]
#[ORM\Repository(repository: UserRepository::class)]
class UserEntity
{

    #[ORM\Column(name: 'id', type: 'int', nullable: false)]
    #[ORM\Identifier]
    private ?int $id = null;
    
    #[ORM\Column(name: 'username', type: 'varchar', length: 100, nullable: false)]
    private ?string $username = null;
    
    public function getId(): ?int
    {
    
        return $this->id;
    
    }
    
    public function setUsername(?string $username): UserEntity
    {
    
        $this->username = $username;
        
        return $this;
    
    }
    
    public function getUsername(): ?string
    {
    
        return $this->username;
    
    }

}

存储库概览

<?php

namespace App\Repositories;

use Codememory\Components\Database\Orm\Repository\AbstractEntityRepository;

class UserRepository extends AbstractEntityRepository
{
    
    public function getUserByUniqueName(): array
    {
    
        $queryBuilder = $this->createQueryBuilder();
        
        $queryBuilder
            ->customSelect()
            ->distinct()
            ->columns(['username'])
            ->from('users');
            
        return $queryBuilder->generateQuery()->getResultAsEntity();
    }
    
}

命令列表

  • db:check-connectin 检查数据库连接
  • db:create-db 如果不存在,则创建连接用户的数据库
  • db:create-table {entity-name} 创建实体表
  • db:update-table {entity-name} 更新实体表结构
  • db:drop-table {entity-name} 删除实体表
  • db:db:list-entities 实体列表
  • db:db:list-repositories 存储库列表
  • make:entity {entity-name-without-suffix} 创建实体

实体构造列表

命名空间构造 - Codememory\Components\Database\Orm\Constructions

  • AI AUTO_INCREMENT
  • Collate 与字符的 COLLATE
  • DefaultValue DEFAULT
  • Entity 不适用于 SQL
  • Identifier AUTO_INCREMENT PRIMARY KEY
  • Join 不适用于 SQL
  • Primary PRIMARY KEY
  • Reference 外键和 REFERENCE
  • Repository 不适用于 SQL
  • Unique UNIQUE
  • Visible VISIBLE|INVISIBLE

Flush

<?php
//! Хорошая скорость добавления записей в базу

use App\Entities\UserEntity;

$userEntity = new UserEntity();

// Добавляем 10 пользователей в commit
for($i = 0; $i < 10; $i++) {
    $userEntity->setUsername(sprintf('user#%s', $i));
    
    $entityManager->commit($userEntity);
}

// Добавляем пользователей в таблицу и очищаем commit
$entityManager->flush();

该文档不详细!