What might be causing Doctrine to generate different class aliases in SELECT clause and WHERE clause? Ex: SELECT e0_.id …WHERE u1_.object_id

What might be causing Doctrine to generate different class aliases in SELECT clause and WHERE clause? Ex: SELECT e0_.id …WHERE u1_.object_id

What might be causing different class aliases to be generated in a simple query?

DQL:

SELECT   log 
FROM     corebasebundleentityuserlog log 
WHERE    log.objectid = :objectId 
AND      log.objectclass = :objectClass 
ORDER BY log.version DESC

SQL

SELECT e0_.id           AS id_0, 
       e0_.action       AS action_1, 
       e0_.logged_at    AS logged_at_2, 
       e0_.object_id    AS object_id_3, 
       e0_.object_class AS object_class_4, 
       e0_.version      AS version_5, 
       e0_.data         AS data_6, 
       e0_.username     AS username_7 
FROM   userlog u1_ 
WHERE  u1_.object_id = ? 
       AND u1_.object_class = ? 
ORDER  BY u1_.version DESC 

I have only “log” alias in the DQL, but suddenly the aliases are changed in the SQL (u1_ -> e0_)

QuerBuilder method (from doctrine-extensions loggable LogEntry repository class)

/**
     * Get the query for loading of log entries
     *
     * @param object $entity
     *
     * @return Query
     */
    public function getLogEntriesQuery($entity)
    {
        $wrapped = new EntityWrapper($entity, $this->_em);
        $objectClass = $wrapped->getMetadata()->name;
        $meta = $this->getClassMetadata();
        $dql = "SELECT log FROM {$meta->name} log";
        $dql .= " WHERE log.objectId = :objectId";
        $dql .= " AND log.objectClass = :objectClass";
        $dql .= " ORDER BY log.version DESC";

        $objectId = (string) $wrapped->getIdentifier();
        $q = $this->_em->createQuery($dql);
        $q->setParameters(compact('objectId', 'objectClass'));

        return $q;
    }

More info:

Im using doctrine extension’s Loggable extension. And i extend both the LogEntry class with my own and same with the Repository class.

doctrine.yml

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        server_version: '5.5' # in case you are using mysql 5.5
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        default_repository_class: CoreBaseBundleEntityRepositoryEntityRepository
        dql:
            string_functions:
                YEAR: DoctrineExtensionsQueryMysqlYear
                DAYOFYEAR: DoctrineExtensionsQueryMysqlDayOfYear
                MONTH: DoctrineExtensionsQueryMysqlMonth
                YEARMONTH: DoctrineExtensionsQueryMysqlYearMonth
                WEEK: DoctrineExtensionsQueryMysqlWeek
                YEARWEEK: DoctrineExtensionsQueryMysqlYearWeek
                DAY: DoctrineExtensionsQueryMysqlDay
                DATE_FORMAT: DoctrineExtensionsQueryMysqlDateFormat
                GROUP_CONCAT: DoctrineExtensionsQueryMysqlGroupConcat
                CONCAT: DoctrineExtensionsQueryMysqlConcat

        mappings:
            translatable:
                type: annotation
                alias: Gedmo
                prefix: GedmoTranslatableEntity
                # make sure vendor library location is correct
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/src/Translatable/Entity"
                is_bundle: false
            gedmo_loggable:
                type: annotation
                prefix: GedmoLoggableEntity
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/src/Loggable/Entity"
                alias: GedmoLoggable # (optional) it will default to the name set for the mapping
                is_bundle: false

My userlog entity:

namespace CoreBaseBundleEntity;

use GedmoLoggableEntityLogEntry;

class UserLog extends LogEntry implements BaseEntityInterface
{
    use BaseEntityTrait;

}

My userlog entity mapping file:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping
    xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
    http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="CoreBaseBundleEntityUserLog" table="userlog"
            repository-class="CoreBaseBundleUserLogRepositoryUserLog">


    </entity>
</doctrine-mapping>

My userlog repository class

namespace CoreBaseBundleUserLogRepository;


use GedmoLoggableEntityRepositoryLogEntryRepository;

class UserLog extends LogEntryRepository
{

}

And this will produce the problematic query:

    public function getLogEntries ($entity)
    {
        /** @var CoreBaseBundleUserLogRepositoryUserLog $repo */
        $repo = $this->em->getRepository(CoreBaseBundleEntityUserLog::class);
        $logs = $repo->getLogEntries($entity);
    }

Source: Symfony Questions

Leave a Reply

Your email address will not be published. Required fields are marked *