Doctrine SQL Server uniqueidentifier does not appear as char or nvarchar when retrieving from database

When I retrieve a record from a database that has a column of type "uniqueidentifier", Doctrine populates it with "null", and not a unique identifier from the database.

Some research and testing has led to a problem with PDO / dblib. With a direct request via PDO, null is returned instead of a unique id.

For reference, http://trac.doctrine-project.org/ticket/1096 , there is a bit of it, however it was updated 11 months ago without comment for permission.

In this case, as mentioned in http://bugs.php.net/bug.php?id=24752&edit=1 , the column should be specified as char. However, Doctrine does not seem to provide its own type of field outside of generating models, which makes it difficult to detect unique types of identifiers and discards them internally when building an SQL query.

Has anyone found a workaround for this?

+3
source share
2 answers

I sent a patch for this, the fix is ​​in PHP 5.3.7 and later. See this bug report for more information http://bugs.php.net/54167

PHP 5.3.7, , Doctrine 1 2.

tds freetds.conf, uniqueidentifier . tds, (, , SQL-, ) - "tds version = 7.0"

+2

2 PHP, # 54167 NULL dblib. , PHP, , . , ( , E3407588-2B77-0000-0200-000000000000). , PHP 5.4.

, uniqueidentifer , Doctrine . Doctrine string(36) guid, Doctrine Query.php. :

// In the schema file
columns:
  userid:
    type: guid
    fixed: false
    unsigned: false
    notnull: false
    primary: true
    autoincrement: false

// Which generates this in the base model
$this->hasColumn('userid', 'guid', null, array(
         'type' => 'guid',
         'fixed' => 0,
         'unsigned' => false,
         'notnull' => false,
         'primary' => true,
         'autoincrement' => false,
));

// Only after you change this in Doctrine/DataDict/Mssql.php on line 192-194
case 'uniqueidentifier':
    $type[] = 'guid'; // changed from 'string'
    $length = 36;

// Then to use the new guid datatype, edit Doctrine/Query.php starting on line 487
foreach ($fields as $fieldName) {
        $columnName = $table->getColumnName($fieldName);
        if (($owner = $table->getColumnOwner($columnName)) !== null &&
                $owner !== $table->getComponentName()) {

            $parent = $this->_conn->getTable($owner);
            $columnName = $parent->getColumnName($fieldName);
            $parentAlias = $this->getSqlTableAlias($componentAlias . '.' . $parent->getComponentName());
            $sql[] = $this->_conn->quoteIdentifier($parentAlias) . '.' . $this->_conn->quoteIdentifier($columnName)
                   . ' AS '
                   . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
        } else {
            /* This new code will get the column definition, look for guid, then cast the column as a string to work around PHP bug 60033.  Everything above this line is unchanged */
            $columnName = $table->getColumnName($fieldName);

            $columnDefinition = $table->getColumnDefinition($columnName);

            if ($columnDefinition['type'] == 'guid') {
                $sql[] = 'CAST(' . $this->_conn->quoteIdentifier($tableAlias) . '.' . $this->_conn->quoteIdentifier($columnName) . ' as VARCHAR(36))'
                   . ' AS '
                   . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);

            } else {
                    // this block is unchanged from the original
                $sql[] = $this->_conn->quoteIdentifier($tableAlias) . '.' . $this->_conn->quoteIdentifier($columnName)
                   . ' AS '
                   . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
            }
        }
    }

, IF, , ( , E3407588- 2B77-0000-0276-3D9E8DE868D6). , Doctrine , Doctrine SQL Server PHP < 5.4.

, Doctrine , , PHP 5.4 beta2 , sqlsrv Doctrine 1.2. , , , mssql_query.

, .

+1

Source: https://habr.com/ru/post/1743921/


All Articles