Common Issues
Common issues and solutions when using Data Helpers.
Introduction
Section titled “Introduction”This guide covers common issues and their solutions:
- Installation Issues - Composer and PHP version problems
- Validation Errors - Validation not working or slow
- Type Casting Problems - Casts not applied correctly
- Performance Issues - Slow DTO creation or high memory usage
- Framework Integration - Laravel and Symfony integration issues
- TypeScript Generation - TypeScript types not generated
Installation Issues
Section titled “Installation Issues”Composer Install Fails
Section titled “Composer Install Fails”Problem:
composer require event4u/data-helpers# Error: Package not foundSolution:
# Make sure you have the correct package namecomposer require event4u/data-helpers
# If still failing, update composercomposer self-updatecomposer clear-cachecomposer require event4u/data-helpersPHP Version Mismatch
Section titled “PHP Version Mismatch”Problem:
Your PHP version (8.1.0) does not satisfy requirement: ^8.2Solution:
# Upgrade to PHP 8.2 or higher# Ubuntu/Debiansudo apt-get install php8.2
# macOS (Homebrew)brew install php@8.2
# Check versionphp -vValidation Issues
Section titled “Validation Issues”Validation Not Working
Section titled “Validation Not Working”Problem:
$dto = UserDTO::fromArray($data);// No validation happensSolution:
// Use validateAndCreate() instead$dto = UserDTO::validateAndCreate($data);
// Or enable auto-validation#[ValidateRequest]class UserDTO extends SimpleDTO { /* ... */ }Validation Rules Not Cached
Section titled “Validation Rules Not Cached”Problem:
# Validation is slowSolution:
# Laravelphp artisan dto:cache
# Symfonybin/console dto:cache
# Verify cache is enabledphp artisan config:show simple-dto.validation.cache_rulesCustom Validation Not Working
Section titled “Custom Validation Not Working”Problem:
#[CustomRule]public readonly string $field;// Rule not appliedSolution:
// Make sure attribute implements ValidationRuleuse event4u\DataHelpers\SimpleDTO\Attributes\ValidationRule;
#[Attribute(Attribute::TARGET_PROPERTY)]class CustomRule implements ValidationRule{ public function rule(): string { return 'custom_rule'; }
public function message(): ?string { return 'The :attribute is invalid.'; }}
// Register custom rule (Laravel)Validator::extend('custom_rule', function($attribute, $value) { return /* validation logic */;});Type Casting Issues
Section titled “Type Casting Issues”Cast Not Applied
Section titled “Cast Not Applied”Problem:
#[Cast(DateTimeCast::class)]public readonly Carbon $date;// Still receives stringSolution:
// Make sure Carbon is importeduse Carbon\Carbon;
// Make sure cast is registereduse event4u\DataHelpers\SimpleDTO\Casts\DateTimeCast;
// Check if cast class existsif (!class_exists(DateTimeCast::class)) { // Install carbon composer require nesbot/carbon}Custom Cast Not Working
Section titled “Custom Cast Not Working”Problem:
#[Cast(MyCast::class)]public readonly string $field;// Cast not appliedSolution:
// Make sure cast implements Cast interfaceuse event4u\DataHelpers\SimpleDTO\Casts\Cast;
class MyCast implements Cast{ public function cast(mixed $value): mixed { return /* cast logic */; }
public function uncast(mixed $value): mixed { return /* uncast logic */; }}Performance Issues
Section titled “Performance Issues”Slow DTO Creation
Section titled “Slow DTO Creation”Problem:
// Takes 0.5 seconds to create DTO$dto = UserDTO::fromArray($data);Solution:
// 1. Enable validation cachingphp artisan dto:cache
// 2. Use lazy loading#[Lazy]public readonly ?array $posts = null;
// 3. Avoid deep nesting// Bad: $dto->a->b->c->d->e// Good: $dto->a->b
// 4. Use batch operations$dtos = DataCollection::make($users, UserDTO::class);High Memory Usage
Section titled “High Memory Usage”Problem:
// Memory usage spikes when creating many DTOsSolution:
// 1. Use chunkingUser::chunk(1000, function($users) { $dtos = DataCollection::make($users, UserDTO::class); // Process dtos});
// 2. Use lazy properties#[Lazy]public readonly ?array $largeData = null;
// 3. Clear cache periodicallyCache::flush();Framework Integration Issues
Section titled “Framework Integration Issues”Laravel: Auto-Validation Not Working
Section titled “Laravel: Auto-Validation Not Working”Problem:
public function store(CreateUserDTO $dto){ // DTO not validated}Solution:
// Add ValidateRequest attribute#[ValidateRequest]class CreateUserDTO extends SimpleDTO { /* ... */ }
// Or use validateAndCreate()public function store(Request $request){ $dto = CreateUserDTO::validateAndCreate($request->all());}Laravel: Eloquent Integration Not Working
Section titled “Laravel: Eloquent Integration Not Working”Problem:
$dto = UserDTO::fromModel($user);// Error: Method not foundSolution:
// Make sure you're using fromArray with model's toArray()$dto = UserDTO::fromArray($user->toArray());
// Or use DataMapper for complex mappings$dto = DataMapper::from($user->toArray()) ->target(UserDTO::class) ->template([ 'name' => 'name', 'email' => 'email', ]) ->map() ->getTarget();Symfony: Doctrine Integration Not Working
Section titled “Symfony: Doctrine Integration Not Working”Problem:
$dto = UserDTO::fromEntity($user);// Error: Method not foundSolution:
// Use DataMapper for entity mapping$dto = DataMapper::from($user) ->target(UserDTO::class) ->template([ 'name' => 'name', 'email' => 'email', ]) ->map() ->getTarget();
// Or convert entity to array first$dto = UserDTO::fromArray([ 'name' => $user->getName(), 'email' => $user->getEmail(),]);Symfony: Security Integration Not Working
Section titled “Symfony: Security Integration Not Working”Problem:
#[WhenGranted('ROLE_ADMIN')]public readonly ?array $adminData = null;// Always nullSolution:
// Pass security context$dto->withContext([ 'security' => $this->security,])->toArray();TypeScript Generation Issues
Section titled “TypeScript Generation Issues”TypeScript Not Generated
Section titled “TypeScript Not Generated”Problem:
php artisan dto:typescript# No files generatedSolution:
# Check output path existsmkdir -p resources/js/types
# Check permissionschmod 755 resources/js/types
# Specify output pathphp artisan dto:typescript --output=resources/js/types
# Check for errorsphp artisan dto:typescript --verboseSee Also
Section titled “See Also”- Quick Start - Get started guide
- Validation - Validation details
- Type Casting - Type casting guide
- Performance - Performance optimization