Entities

Entities are a part of Doctrine's ORM. In general terms, they are PHP objects that represent database records.

If you want to learn how to use them, please refer to Doctrine documentation. They work the same as in synchronous PHP environments, with a few caveats related to obtaining DBAL connections. You can learn more about those in the Doctrine feature page.

Here, we will describe how you can integrate them with Resonance's features.

Usage

Obtaining from Entity Managers

You can learn more at Entity Managers page.

Controller Parameter Injection

You can use the Distantmagic\Resonance\Attribute\DoctrineEntityRouteParameter to use an entity as Controllers parameter.

Both intent and lookupField are optional. By default CrudAction::Read and "id" are used, respectively:

app/HttpResponder/BlogPostShow.php
php
<?php namespace App\HttpResponder; use App\DoctrineEntity\BlogPost; use App\HttpRouteSymbol; use Distantmagic\Resonance\Attribute\DoctrineEntityRouteParameter; use Distantmagic\Resonance\Attribute\RespondsToHttp; use Distantmagic\Resonance\Attribute\Singleton; use Distantmagic\Resonance\CrudAction; use Distantmagic\Resonance\HttpInterceptableInterface; use Distantmagic\Resonance\HttpResponder; use Distantmagic\Resonance\HttpResponder\HttpController; use Distantmagic\Resonance\RequestMethod; use Distantmagic\Resonance\SingletonCollection; use Distantmagic\Resonance\TwigTemplate; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; #[RespondsToHttp( method: RequestMethod::GET, pattern: '/blog/{blog_post_slug}', routeSymbol: HttpRouteSymbol::BlogPostShow, )] #[Singleton(collection: SingletonCollection::HttpResponder)] final readonly class BlogPostShow extends HttpController { public function createResponse( #[DoctrineEntityRouteParameter( from: 'blog_post_slug', intent: CrudAction::Read, lookupField: 'slug', )] BlogPost $blogPost, ): HttpInterceptableInterface { return new TwigTemplate('turbo/website/blog_post.twig', [ 'blog_post' => $blogPost, ]); } }

If you use an entity in a controller parameters, you might also need to add Authorization gate to determine who can use that entity.

Edit on GitHub