przemo_li
przemo_li

Reputation: 4053

Symfony custom doctrine datetime type not working

I have following configuration where I try to handle SQL Server 2008 running in 2005 compatibility mode, datetime quirks.

=== config.yml
# Doctrine Configuration
doctrine:
  dbal:
    types:
      datetime2005: AppBundle\ORM\DBAL\DateTime2005Type



=== DateTime2005Type.php
namespace AppBundle\ORM\DBAL;

use Doctrine\DBAL\Types\DateTimeType as BaseDateTimeType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class DateTime2005Type extends BaseDateTimeType
{
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        die();
    }
}

Do note this die() there. It should stop execution of my app, right?

=== SomeEntity.php
/**
 * @ORM\Column(name="ErzDate", type="datetime2005", nullable=true)
 */
private $createdAt;

ss

=== Rendered SQL & error:
An exception occurred while executing 'SELECT count(DISTINCT m0_.Ident) AS sclr0 FROM MedionOA3Requests m0_ WHERE m0_.Project LIKE ? AND m0_.ErzDate IS NOT NULL AND m0_.ErzDate >= ?' with params ["MANUALQUERY", "2016-02-13 00:00:00"]:

SQLSTATE [22007, 242]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Bei der Konvertierung eines nvarchar-Datentyps in einen datetime-Datentyp liegt der Wert außerhalb des gültigen Bereichs. 

So my custom datetime was not loaded or, I'm implementing it wrong. Please, help.

Upvotes: 1

Views: 1092

Answers (1)

NDM
NDM

Reputation: 6830

Custom types only work for persist and select expressions, not for WHERE clauses. This means the object will have the converted value after hydration.

From the docs:

When using DQL queries, the convertToPHPValueSQL and convertToDatabaseValueSQL methods only apply to identification variables and path expressions in SELECT clauses. Expressions in WHERE clauses are not wrapped! If you want to use Point values in WHERE clauses, you have to implement a user defined function.

Maybe you can solve it by using a custom function as they suggest.

Upvotes: 2

Related Questions