davidkmenta/doctrine-simple-array-types

Extended simple array types for Doctrine

1.1.0 2018-10-18 18:29 UTC

This package is auto-updated.

Last update: 2024-04-19 09:19:23 UTC


README

A Doctrine field type for simple arrays of integers, floats and strings.

Description

These types mainly solve two common problems:

  • simple_string_array solves the problem with strings containing a comma symbol. Such strings cannot be persisted in the simple_array type provided by the Doctrine.
  • simple_integer_array and simple_float_array extends the simple_array type from the Doctrine. These types solve the problem when a persisted integer or float is returned from the database as a string.

Installation

Run the following command:

composer require davidkmenta/doctrine-simple-array-types

Examples

To configure Doctrine to use this set of types, you'll need to set up the following in your bootstrap:

<?php

\Doctrine\DBAL\Types\Type::addType('simple_string_array', 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleStringArrayType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('simple_string_array', 'simple_string_array');

\Doctrine\DBAL\Types\Type::addType('simple_integer_array', 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleIntegerArrayType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('simple_integer_array', 'simple_integer_array');

\Doctrine\DBAL\Types\Type::addType('simple_float_array', 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleFloatArrayType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('simple_float_array', 'simple_float_array');

Or, if you're using the Symfony, set types in your Doctrine configuration file (eg: doctrine.yml):

doctrine:
    dbal:
        types:
            simple_string_array: 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleStringArrayType'
            simple_integer_array: 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleIntegerArrayType'
            simple_float_array: 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleFloatArrayType'

Then, in your entities, you may annotate properties by setting the @Column type to simple_string_array, simple_integer_array or simple_float_array.