digitalstate/platform-entity-bundle
Installation:
composer require digitalstate/platform-entity-bundle
Ensure DigitalState\PlatformEntityBundle\DigitalStatePlatformEntityBundle::class is registered in config/bundles.php.
First Use Case:
Extend an existing entity (e.g., App\Entity\Product) with platform-specific attributes:
use DigitalState\PlatformEntityBundle\Entity\PlatformEntityInterface;
class Product implements PlatformEntityInterface
{
use PlatformEntityTrait;
// Your existing fields...
}
Run migrations (php bin/console doctrine:migrations:diff + php bin/console doctrine:migrations:migrate).
Key Files to Review:
src/Entity/PlatformEntityTrait.php (core trait for entity behavior).src/DependencyInjection/ (configuration and services).src/Repository/ (custom repository methods).PlatformEntityTrait to add platform-specific fields (e.g., createdAt, updatedAt, deletedAt soft-delete, or custom metadata).class Article implements PlatformEntityInterface
{
use PlatformEntityTrait;
#[ORM\Column(type: 'string', length: 255)]
private ?string $slug = null;
// Automatically gets platform fields (e.g., `createdAt`, `isActive`).
}
DataResolver services.// In a controller or service:
$resolver = $this->container->get('digitalstate_platform_entity.data_resolver.article');
$data = $resolver->resolve($articleEntity);
FormHandler interfaces.// Define a custom handler:
class ArticleFormHandler implements FormHandlerInterface
{
public function handle(Article $entity, array $data): void
{
$entity->setSlug(Str::slug($data['title']));
}
}
// Register in services.yaml:
services:
App\Form\Handler\ArticleFormHandler:
tags: ['digitalstate.platform_entity.form_handler']
class ArticleRepository extends ServiceEntityRepository
{
public function findActiveWithSlug(string $slug): ?Article
{
return $this->createQueryBuilder('a')
->andWhere('a.slug = :slug')
->andWhere('a.isActive = :active')
->setParameter('slug', $slug)
->setParameter('active', true)
->getQuery()
->getOneOrNullResult();
}
}
class ArticleController extends AbstractPlatformController
{
public function index(DataResolverInterface $resolver): Response
{
$articles = $resolver->resolveCollection(Article::class);
return $this->json($articles);
}
}
prePersist, preUpdate, etc., to auto-set platform fields (e.g., createdAt).
$entityManager->getEventManager()->addEventSubscriber(new PlatformEntitySubscriber());
FormHandler for seamless validation and processing.
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
$this->formHandler->handle($article, $form->getData());
DataResolver to customize serialization/deserialization.
# config/api_platform/resources.yaml
resources:
App\Entity\Article:
collectionOperations:
get:
method: 'GET'
path: '/articles'
controller: 'digitalstate_platform_entity.controller.article'
Trait Overload:
PlatformEntityTrait methods unless necessary. Prefer composition (e.g., add custom methods to the entity class).Migration Conflicts:
PlatformEntityTrait to an existing entity may require manual migration adjustments (e.g., createdAt/updatedAt columns).php bin/console doctrine:schema:update --dump-sql to preview changes.Circular Dependencies:
OroEntityBundle is installed. If missing, install it first:
composer require oro/platform
Data Resolver Caching:
php bin/console cache:clear
# config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
$dispatcher->addListener(PlatformEntityEvents::PRE_PERSIST, function ($event) {
error_log('Pre-persist triggered for: ' . get_class($event->getEntity()));
});
config/packages/digitalstate_platform_entity.yaml:
digitalstate_platform_entity:
fields:
created_at: 'creationDate' # Custom column name
digitalstate_platform_entity:
soft_delete: true
$entityManager->getRepository(Article::class)->restore($article);
use DigitalState\PlatformEntityBundle\Attribute\PlatformAttribute;
#[PlatformAttribute]
class CustomAttribute extends Attribute
{
public function load(PlatformEntityInterface $entity, array $data): void
{
$entity->setCustomField($data['value']);
}
}
PlatformEntityEvents::POST_UPDATE):
class CustomSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
PlatformEntityEvents::POST_UPDATE => 'onPostUpdate',
];
}
public function onPostUpdate(PlatformEntityEvent $event): void
{
// Custom logic after update.
}
}
class PlatformArticleRepository extends ArticleRepository
{
public function __construct(PlatformEntityManager $em)
{
parent::__construct($em);
}
public function findWithPlatformData(): array
{
// Custom query with platform fields.
}
}
How can I help you explore Laravel packages today?