Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added field specific options for converting data between PHP and DB #661

Closed
wants to merge 1 commit into from
Closed

Conversation

yannickl88
Copy link

Added option parameter so field specific data can be passed when converting data for types. This is needed in cases like enum types or more generic types where you want field specific options for conversion.

This would give developers more freedom in creating custom domain-specific types.

Our (simplified) use-case is:

class EnumType extends Type
{
    const ENUM = 'enum';

    public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return 'Enum';
    }

    public function convertToPHPValue($value, AbstractPlatform $platform, array $options = [])
    {
        return (null === $value) ? null : new $options['class']((int) $value);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform, array $options = [])
    {
        return $value;
    }

    public function getName()
    {
        return self::ENUM;
    }
}

This way you can define a generic Enum and configure the matching type of the enum in the field itself. The end goal is to make use of this change in the ORM as follows:

class Entity
{
    /**
    * @ORM\Column(
    *     name         = "bar",
    *     type         = "enum",
    *     type_options = {
    *         "class" = "\Foo\Bar\FooBarEnum"
    *     }
    * )
    * @var FooBarEnum
    */
    private $foo;
}

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DBAL-973

We use Jira to track the state of pull requests and the versions they got
included in.

@Ocramius
Copy link
Member

Types are lightweight and are not supposed to be configurable by design.
Introducing options in types makes them more complicated than what is needed.

Also, please don't use ENUM, as it is not portable: http://stackoverflow.com/questions/8750724/what-do-you-use-instead-of-enum-in-doctrine2/9057352#9057352

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants