Что может быть причиной того, что Doctrine генерирует разные псевдонимы классов в предложении SELECT и предложении WHERE? Пример: ВЫБРАТЬ e0_.id ... ГДЕ u1_.object_id - PullRequest
2 голосов
/ 13 февраля 2020

Что может быть причиной создания псевдонимов разных классов в простом запросе?

DQL:

SELECT   log 
FROM     core\basebundle\entity\userlog 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 

У меня есть только псевдоним "log" в DQL, но внезапно псевдонимы меняются в SQL (u1_ -> e0 _)

QuerBuilder метод (с doctrine -extensions loggable Класс репозитория LogEntry)

/**
     * 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;
    }

Дополнительная информация:

Я использую doctrine расширение Loggable расширения. И я расширяю оба класса LogEntry своим собственным и таким же классом Repository.

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: Core\BaseBundle\Entity\Repository\EntityRepository
        dql:
            string_functions:
                YEAR: DoctrineExtensions\Query\Mysql\Year
                DAYOFYEAR: DoctrineExtensions\Query\Mysql\DayOfYear
                MONTH: DoctrineExtensions\Query\Mysql\Month
                YEARMONTH: DoctrineExtensions\Query\Mysql\YearMonth
                WEEK: DoctrineExtensions\Query\Mysql\Week
                YEARWEEK: DoctrineExtensions\Query\Mysql\YearWeek
                DAY: DoctrineExtensions\Query\Mysql\Day
                DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat
                GROUP_CONCAT: DoctrineExtensions\Query\Mysql\GroupConcat
                CONCAT: DoctrineExtensions\Query\Mysql\Concat

        mappings:
            translatable:
                type: annotation
                alias: Gedmo
                prefix: Gedmo\Translatable\Entity
                # 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: Gedmo\Loggable\Entity
                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

Моя сущность пользовательского журнала:

namespace Core\BaseBundle\Entity;

use Gedmo\Loggable\Entity\LogEntry;

class UserLog extends LogEntry implements BaseEntityInterface
{
    use BaseEntityTrait;

}

Файл сопоставления сущности моего пользовательского журнала:

<?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="Core\BaseBundle\Entity\UserLog" table="userlog"
            repository-class="Core\BaseBundle\UserLog\Repository\UserLog">


    </entity>
</doctrine-mapping>

Класс хранилища моего пользовательского журнала

namespace Core\BaseBundle\UserLog\Repository;


use Gedmo\Loggable\Entity\Repository\LogEntryRepository;

class UserLog extends LogEntryRepository
{

}

И это создаст проблемный запрос c:

    public function getLogEntries ($entity)
    {
        /** @var \Core\BaseBundle\UserLog\Repository\UserLog $repo */
        $repo = $this->em->getRepository(\Core\BaseBundle\Entity\UserLog::class);
        $logs = $repo->getLogEntries($entity);
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...