magmasoftwareengineering / doctrine-base
Base structures for Doctrine-based projects
Package info
bitbucket.org/magmasoftwareengineering/doctrine-base
pkg:composer/magmasoftwareengineering/doctrine-base
Requires
- php: >=8.3
- doctrine/dbal: ^3.10
- doctrine/doctrine-laminas-hydrator: ^3.7
- doctrine/migrations: ^3.9
- doctrine/orm: ^3.6
- gedmo/doctrine-extensions: ^3.21
- league/fractal: ^0.20 || ^0.21
- magmasoftwareengineering/base: ^3.1 || ^4.0
Requires (Dev)
- codeception/codeception: ^5.3
- codeception/module-asserts: ^3.3
- roave/security-advisories: dev-latest
README
Version: 3.x | PHP: >=8.3 | License: MIT
Overview
The Doctrine Base Library (magmasoftwareengineering/doctrine-base) builds upon the Base Library to provide Doctrine ORM/DBAL integration, entity mapping, transformation utilities, and repository patterns for database-driven applications.
This library is the middle layer in the Magma Software Engineering library hierarchy, designed for projects requiring persistent storage and entity management.
Key Features
Entity Management
- AbstractEntity - Base class with common entity functionality
- AbstractBaseEntity - Extended base with additional utilities
- SettingEntity - Pre-built entity for application settings storage
- StatusAwareTrait / StatusAwareInterface - Add status tracking to entities
Repository Pattern
- AbstractRepository - Base repository with common query operations
- RepositoryFactory - Dynamic repository generation
- SettingRepository - Specialized repository for application settings
- DoctrineHydratorAwareRepositoryInterface - Hydrator integration
Data Transformation
- AbstractTransformer - Base transformer for entity-to-DTO conversions
- EntityTransformerService - Service for managing multiple transformers
- TransformableInterface - Mark entities as transformable
Database Types
- TimestampType - Custom Doctrine DBAL type for timestamp fields
- TinyintType - Custom Doctrine DBAL type for tinyint fields
Services
- AbstractService - Base service class with dependency injection
- DoctrineAwareServiceInterface - Services that need entity manager access
Dependencies
magmasoftwareengineering/base: ^3.1 || ^4.0 (Foundation library)
doctrine/dbal: ^3.10 (Database abstraction)
doctrine/doctrine-laminas-hydrator: ^3.7 (Entity hydration)
doctrine/migrations: ^3.9 (Database migrations)
doctrine/orm: ^3.6 (Object-relational mapping)
gedmo/doctrine-extensions: ^3.21 (Behaviour extensions: slugs, timestamps, etc.)
league/fractal: ^0.20 || ^0.21 (Data transformation)
Installation
composer require magmasoftwareengineering/doctrine-base:^3.0
Requires:
composer require magmasoftwareengineering/base:^3.0
Usage Examples
Creating an Entity
use MagmaSoftwareEngineering\DoctrineBase\Entity\AbstractBaseEntity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'users')]
class User extends AbstractBaseEntity {
#[ORM\Column(type: 'string')]
private string $email;
#[ORM\Column(type: 'string')]
private string $name;
// Getters and setters...
}
Creating a Repository
use MagmaSoftwareEngineering\DoctrineBase\Repository\AbstractRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
class UserRepository extends AbstractRepository {
public function __construct(EntityManagerInterface $em) {
parent::__construct($em, $em->getClassMetadata(User::class));
}
public function findActiveUsers() {
return $this->findBy(['status' => 'active']);
}
}
Entity Transformation
use MagmaSoftwareEngineering\DoctrineBase\Transformer\AbstractTransformer;
class UserTransformer extends AbstractTransformer {
public function transform(User $user) {
return [
'id' => $user->getId(),
'email' => $user->getEmail(),
'name' => $user->getName(),
];
}
}
Service Layer
use MagmaSoftwareEngineering\DoctrineBase\Service\AbstractService;
class UserService extends AbstractService {
public function createUser(string $email, string $name): User {
$user = new User();
$user->setEmail($email);
$user->setName($name);
$this->em->persist($user);
$this->em->flush();
return $user;
}
}
Architecture
This library provides:
- Persistence Layer - Entity management through Doctrine ORM
- Data Access Patterns - Repository and Service abstractions
- Data Transformation - Entity-to-DTO conversion
- Database Extensions - Custom types and behaviors (timestamps, slugs, etc. via Gedmo)
Dependency Chain
magmasoftwareengineering/base
↓
magmasoftwareengineering/doctrine-base
↓
magmasoftwareengineering/slim-base
Related Libraries
| Library | Purpose | When to Use |
|---|---|---|
| Base Library | Core utilities, logging, DI | All projects - use as foundation |
| Doctrine Base | ORM entities, repositories | Database-driven applications |
| Slim Base Library | Slim 4 HTTP framework integration | Web applications with Slim 4 |
Next Steps
- Building a web application? → Slim Base Library (includes Doctrine Base)
- Need API scaffolding? Check the slim-skeleton-api project for examples
- Looking for batch processing? Use AbstractCommand from Base Library
Support & Contributing
For issues or contributions, please contact the maintainer:
- Jeremy Coates - hello@phpcodemonkey.me.uk
License
MIT License - See LICENSE file for details