neonus/db-layer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

neonus/db-layer

最新稳定版本:v1.1.0

Composer 安装命令:

composer require neonus/db-layer

包简介

A custom DBAL-based repository pattern.

README 文档

README

A lightweight PHP database abstraction layer built on top of Doctrine DBAL with a repository pattern implementation.

Overview

This package provides a clean, extensible database layer for PHP applications using:

  • Doctrine DBAL for database interactions
  • Repository Pattern for data access abstraction
  • Entity pattern for object-oriented data representation
  • Factory pattern for entity creation

Features

  • 🔧 BaseRepository - Abstract repository with common database operations
  • 🏗️ BaseEntity - Abstract entity class with standard properties and methods
  • 📦 EntityFactory - Factory for creating entity instances from data
  • 🔌 Extensible interfaces - Easy to implement custom repositories and entities
  • Type-safe operations - Parameter type handling for DBAL

Installation

composer require neonus/db-layer

Requirements

  • PHP 7.4+
  • Doctrine DBAL ^3.0 or ^4.0
  • Symfony Dependency Injection ^8.0

Quick Start

1. Create an Entity

Extend BaseEntity to define your domain model:

namespace App\Entity;

use Neonus\DbLayer\Entity\BaseEntity;

class User extends BaseEntity
{
    protected string $name = '';
    protected string $email = '';
    protected string $url = '';

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

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

    // Add more getters/setters...
}

2. Create a Repository

Extend BaseRepository to implement data access logic:

namespace App\Repository;

use Neonus\DbLayer\Repository\BaseRepository;
use App\Entity\User;

class UserRepository extends BaseRepository
{
    public const TABLE_NAME = 'users';
    public const ENTITY_NAME = User::class;

    // Inherit basic CRUD operations, or add custom queries
}

3. Use the Repository

$userRepository = new UserRepository($connection, $entityFactory);

// Retrieve a user by ID
$user = $userRepository->get(1);

// Retrieve a user by URL
$user = $userRepository->getByUrl('john-doe');

// Create/Update a user
$user->setName('John Doe');
$userRepository->persist($user);

// Delete all records
$userRepository->deleteAll();

API Reference

BaseRepository

Methods

  • get(int $id): BaseEntity|bool - Fetch a single entity by ID
  • getByUrl(string $url): BaseEntity|bool - Fetch a single entity by URL
  • persist(BaseEntity $entity, bool $forceInsert = false): int - Save or update an entity
  • deleteAll(): int - Delete all records from the table
  • getDb(): Connection - Access the DBAL connection directly

BaseEntity

Properties

  • id - Primary key (auto-managed)
  • created_at - Creation timestamp (DateTime)

Methods

  • getId(): int - Get the entity ID
  • setId(int $id): void - Set the entity ID
  • getCreatedAt(): DateTime - Get creation timestamp
  • setCreatedAt(mixed $created_at): void - Set creation timestamp
  • toArray(): array - Convert entity to array

Project Structure

src/
├── Entity/
│   └── BaseEntity.php              # Base entity class
├── Repository/
│   └── BaseRepository.php          # Base repository class
├── Lib/
│   └── EntityFactory.php           # Entity factory
└── Interfaces/
    ├── RepositoryInterface.php     # Repository interface
    ├── HydratationInterface.php    # Hydration interface

License

Proprietary - NEONUS, s.r.o.

Author

NEONUS, s.r.o. (info@neonus.sk)

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2025-12-31