CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 5.3 Chiffon API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 5.3
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
      • Association
      • Attribute
      • Behavior
      • Exception
      • Locator
      • Query
      • Rule
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class DtoMapper

Maps array data to DTO objects using reflection.

This mapper enables the projectAs() query method to hydrate results directly into DTO objects instead of entities. It uses PHP 8 features:

  • Type hints on constructor parameters to detect nested DTOs
  • #[CollectionOf] attribute to specify DTO type for array collections
  • Named arguments for construction

Example DTO

readonly class UserDto {
    public function __construct(
        public int $id,
        public string $username,
        public ?RoleDto $role = null,
        #[CollectionOf(CommentDto::class)]
        public array $comments = [],
    ) {}
}

Usage with Query

$users = $this->Users->find()
    ->contain(['Roles', 'Comments'])
    ->projectAs(UserDto::class)
    ->all();
Namespace: Cake\ORM

Property Summary

  • $cache protected static
    array<string, array{params: array<string, array{name: string, nullable: bool, hasDefault: bool, default: mixed, dtoClass: (class-string|null, collectionOf: (class-string|null)}>}>

    Cached reflection info per class.

Method Summary

  • analyzeParameter() protected

    Analyze a constructor parameter for DTO mapping info.

  • clearCache() public static

    Clear the reflection cache.

  • getClassInfo() protected

    Get cached class info via reflection.

  • map() public

    Map array data to a DTO instance.

Method Detail

analyzeParameter() ¶ protected

analyzeParameter(ReflectionParameter $param): array{name: string, nullable: bool, hasDefault: bool, default: mixed, dtoClass: (class-string|null, collectionOf: (class-string|null)}

Analyze a constructor parameter for DTO mapping info.

Parameters
ReflectionParameter $param

The parameter to analyze

Returns
array{name: string, nullable: bool, hasDefault: bool, default: mixed, dtoClass: (class-string|null, collectionOf: (class-string|null)}

clearCache() ¶ public static

clearCache(): void

Clear the reflection cache.

Useful for testing or when classes are reloaded.

Returns
void

getClassInfo() ¶ protected

getClassInfo(class-string $class): array{params: array<string, array{name: string, nullable: bool, hasDefault: bool, default: mixed, dtoClass: (class-string|null, collectionOf: (class-string|null)}>}

Get cached class info via reflection.

Parameters
class-string $class

The class to analyze

Returns
array{params: array<string, array{name: string, nullable: bool, hasDefault: bool, default: mixed, dtoClass: (class-string|null, collectionOf: (class-string|null)}>}

map() ¶ public

map(array<string, mixed> $data, class-string<T> $dtoClass): T

Map array data to a DTO instance.

Templates
T of object
Parameters
array<string, mixed> $data

The source data (typically from ORM)

class-string<T> $dtoClass

The target DTO class

Returns
T

Property Detail

$cache ¶ protected static

Cached reflection info per class.

Type
array<string, array{params: array<string, array{name: string, nullable: bool, hasDefault: bool, default: mixed, dtoClass: (class-string|null, collectionOf: (class-string|null)}>}>
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs