Skip to content

Conversation

@IndraGunawan
Copy link
Contributor

@IndraGunawan IndraGunawan commented Jan 1, 2024

fixed: #1002
replace: #1021

sample custom type

<?php

declare(strict_types=1);

namespace App\Doctrine\DBAL\Types;

use Cake\Chronos\Chronos;
use DateTimeImmutable;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeImmutableType;

class ChronosDateTimeType extends DateTimeImmutableType
{
    final public const CHRONOS_DATETIME = 'chronos_datetime';

    /**
     * {@inheritDoc}
     */
    public function convertToPHPValue($value, AbstractPlatform $platform): ?Chronos
    {
        if (null === $value) {
            return null;
        }

        $dateTime = parent::convertToPHPValue($value, $platform);

        return Chronos::instance($dateTime);
    }

    /*
     * ....
     */
}

this change will get convertToPHPValue function return type and set it as property type
if no return type defined on convertToPHPValue function then the property will not have any type hint

@IndraGunawan
Copy link
Contributor Author

IndraGunawan commented Jan 1, 2024

CI error is not related

@IndraGunawan
Copy link
Contributor Author

@jrushlow @weaverryan any chance to review this?

@jrushlow jrushlow changed the title handle property type for custom doctrine type [DoctrineHelper] handle property type for custom doctrine type Feb 27, 2024
Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. A couple minor comments to improve readability.

@jrushlow jrushlow added Feature New Feature Status: Needs Work Additional work is needed labels Feb 27, 2024
Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @IndraGunawan!

@jrushlow jrushlow added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Work Additional work is needed labels Mar 4, 2024
@jrushlow jrushlow merged commit 6f1e210 into symfony:main Mar 4, 2024
@jrushlow jrushlow mentioned this pull request Mar 4, 2024
@IndraGunawan IndraGunawan deleted the custom-property-type branch March 4, 2024 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature New Feature Status: Reviewed Has been reviewed by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow doctrine custom types to register their php class to makerbundle for generating Type Hint

2 participants