AnsiQuoteStrategy does not convert field names to column names for identifiers #7028

Open
opened 2026-01-22 15:43:23 +01:00 by admin · 0 comments
Owner

Originally created by @ViktorCollin on GitHub (Aug 29, 2022).

Bug Report

Q A
BC Break no
Version 2.5+

Summary

If a identifier column name for a table differs from the corresponding field name the AnsiQuoteStrategy::getIdentifierColumnNames will return the field name that is not usable in SQL

Current behavior

Given and entity with a primary key column user_id and a field name userId.

$user = $entiryManager->getRepository(User::class)->find('u1');
$entiryManager->remove($user);
$entiryManager->flush();

The following code results in an PDOException caused by SQL query: 'DELETE FROM user WHERE userId = ?' with params ["u1"]

Expected query would have been: 'DELETE FROM user WHERE user_id = ?' with params ["u1"]

How to reproduce

Entity/User.php

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User
{
    /**
     * @var string
     * @ORM\Column(name="user_id", type="string", length=36, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    public string $userId;
}

test.php

<?php
namespace Test;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Mapping\AnsiQuoteStrategy;
use Entity\User;

require_once 'vendor/autoload.php';
require_once 'Entity/User.php';

$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxy');
$config->setProxyNamespace('DoctrineProxies');
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([__DIR__ . '/Entity'], false));
$config->setQuoteStrategy(new AnsiQuoteStrategy());

$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
$connection->executeStatement("CREATE TABLE user (user_id varchar(36), PRIMARY KEY (user_id))");
$connection->executeStatement("INSERT INTO user VALUES ('u1')");

$em = EntityManager::create($connection, $config);

$user = $em->getRepository(User::class)->find('u1');

$em->remove($user);
$em->flush();

php test.php

Originally created by @ViktorCollin on GitHub (Aug 29, 2022). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.5+ #### Summary If a identifier column name for a table differs from the corresponding field name the `AnsiQuoteStrategy::getIdentifierColumnNames` will return the field name that is not usable in SQL #### Current behavior Given and entity with a primary key column user_id and a field name userId. ``` $user = $entiryManager->getRepository(User::class)->find('u1'); $entiryManager->remove($user); $entiryManager->flush(); ``` The following code results in an `PDOException` caused by SQL query: `'DELETE FROM user WHERE userId = ?' with params ["u1"]` Expected query would have been: `'DELETE FROM user WHERE user_id = ?' with params ["u1"]` #### How to reproduce Entity/User.php ``` <?php namespace Entity; use Doctrine\ORM\Mapping as ORM; /** * User * * @ORM\Table(name="user") * @ORM\Entity */ class User { /** * @var string * @ORM\Column(name="user_id", type="string", length=36, nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ public string $userId; } ``` test.php ``` <?php namespace Test; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Configuration; use Doctrine\DBAL\DriverManager; use Doctrine\ORM\Mapping\AnsiQuoteStrategy; use Entity\User; require_once 'vendor/autoload.php'; require_once 'Entity/User.php'; $config = new Configuration(); $config->setProxyDir(__DIR__ . '/Proxy'); $config->setProxyNamespace('DoctrineProxies'); $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([__DIR__ . '/Entity'], false)); $config->setQuoteStrategy(new AnsiQuoteStrategy()); $connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]); $connection->executeStatement("CREATE TABLE user (user_id varchar(36), PRIMARY KEY (user_id))"); $connection->executeStatement("INSERT INTO user VALUES ('u1')"); $em = EntityManager::create($connection, $config); $user = $em->getRepository(User::class)->find('u1'); $em->remove($user); $em->flush(); ``` `php test.php`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7028