sonata-project / SonataDoctrineORMAdminBundle

Integrate Doctrine ORM into the SonataAdminBundle
https://docs.sonata-project.org/projects/SonataDoctrineORMAdminBundle
MIT License
445 stars 344 forks source link

3.2.0 doctrine-orm-bundle error with assosiation-key entities in admin-list #776

Closed multifinger closed 6 years ago

multifinger commented 6 years ago

Subject

I've got entity with foreing relation as id:

Myorg\SearchBundle\Entity\RegionSettings:
    type:                                       entity
    table:                                      search__region_settings
    repositoryClass:                            Myorg\SearchBundle\Repository\RegionSettingsRepository
    id:
        Region:                                 { associationKey: true }
    fields:
        searchDepartureId:                      { type: bigint, column: search_departure_id }
    oneToOne:
        Region:
            targetEntity:                       Myorg\DictionaryBundle\Entity\Region
            joinColumn:                         { name: id, referencedColumnName: id }
    lifecycleCallbacks:                         {  }

Sonata admin list page for this entity worket well,
but after composer update (sonata-doctrine-orm from 3.1.6 to 3.2.0) it stop worked with error: Invalid PathExpression. Must be a StateFieldPathExpression. Error is somewhere in query: SELECT DISTINCT IDENTITY(o.Region) as Region, o.Region FROM Myorg\SearchBundle\Entity\RegionSettings o ORDER BY o.Region ASC

I've updated to "sonata-project/doctrine-orm-admin-bundle": "3.1.*" and error is gone

Steps to reproduce

1) Create entity with assosiation-key, 2) create admin for that entity, 3) use 3.2.0 of sonata doctrine orm admin bunlde

Expected results

Admin list working

Actual results

Admin list NOT working

Environment

ubuntu 16.04

Sonata packages

sonata-project/admin-bundle              3.28.0                   The missing Symfony Admin Generator
sonata-project/block-bundle              3.8.0                    Symfony SonataBlockBundle
sonata-project/cache                     2.0.1                    Cache library
sonata-project/classification-bundle     3.5.0                    Symfony SonataClassificationBundle
sonata-project/core-bundle               3.7.1                    Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.3.0                    Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.0.2                    Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.2.0                    Symfony Sonata / Integrate Doctrine ORM into the ...
sonata-project/easy-extends-bundle       2.3.0                    Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.8.0                    Lightweight Exporter library
sonata-project/google-authenticator      2.0.0                    Library to integrate Google Authenticator into a ...
sonata-project/intl-bundle               2.4.0                    Symfony SonataIntlBundle
sonata-project/media-bundle              3.10.0                   Symfony SonataMediaBundle
sonata-project/notification-bundle       3.2.0                    Symfony SonataNotificationBundle
sonata-project/user-bundle               3.6.0                    Symfony SonataUserBundle

Symfony packages

$ composer show --latest 'symfony/*'
symfony/monolog-bundle                   v2.12.1                  Symfony MonologBundle
symfony/phpunit-bridge                   v2.8.32                  Symfony PHPUnit Bridge
symfony/polyfill-apcu                    v1.6.0                   Symfony polyfill backporting apcu_* functions to ...
symfony/polyfill-intl-icu                v1.6.0                   Symfony polyfill for intl's ICU-related data and ...
symfony/polyfill-mbstring                v1.6.0                   Symfony polyfill for the Mbstring extension
symfony/polyfill-php54                   v1.6.0                   Symfony polyfill backporting some PHP 5.4+ featur...
symfony/polyfill-php55                   v1.6.0                   Symfony polyfill backporting some PHP 5.5+ featur...
symfony/polyfill-php56                   v1.6.0                   Symfony polyfill backporting some PHP 5.6+ featur...
symfony/polyfill-php70                   v1.6.0                   Symfony polyfill backporting some PHP 7.0+ featur...
symfony/polyfill-util                    v1.6.0                   Symfony utilities for portability of PHP codes
symfony/security-acl                     v3.0.0                   Symfony Security Component - ACL (Access Control ...
symfony/swiftmailer-bundle               v2.6.7                   Symfony SwiftmailerBundle
symfony/symfony                          v2.8.32                  The Symfony PHP framework

PHP version

$ php -v
PHP 7.1.12-2+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Dec  7 2017 20:12:04) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.12-2+ubuntu16.04.1+deb.sury.org+2, Copyright (c) 1999-2017, by Zend Technologies
dmarkowicz commented 6 years ago

Will be fixed by #768.

rikvanderkemp commented 6 years ago

I'm running into the same issue, even with 3.3 of this bundle.

My Entity:

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Entity(repositoryClass="MenuRepository")
 * @ORM\Table()
 * @ORM\ChangeTrackingPolicy(value="DEFERRED_EXPLICIT")
 */
class Menu
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $subtitle;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $url;

    /**
     * @ORM\Column(type="boolean", nullable=false, options={"default": false})
     */
    private $active = false;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(name="lft", type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(name="rgt", type="integer")
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\ManyToOne(targetEntity="MenuBundle\Entity\Menu")
     * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
     */
    private $root;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="MenuBundle\Entity\Menu", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="MenuBundle\Entity\Menu", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;
}

In my admin:

    /**
     * {@inheritdoc}
     */
    public function createQuery($context = 'list')
    {
        $em = $this->modelManager->getEntityManager($this->getClass());

        /** @var $cmd \Doctrine\Common\Persistence\Mapping\ClassMetadata */
        $classMetaData = $em->getMetadataFactory()->getMetadataFor($this->getClass());

        /** @var QueryBuilder $queryBuilder */
        $queryBuilder = $em
            ->createQueryBuilder('m')
            ->select('m')
            ->from($this->getClass(), 'm');

            $queryBuilder->orderBy('m.root, m.lft');

        return new ProxyQuery($queryBuilder);
    }

Results in:

An exception has been thrown during the rendering of a template ("[Semantical Error] line 0, col 24 near 'root, m.lft,': Error: Invalid PathExpression. Must be a StateFieldPathExpression.").

Php version:

PHP 7.1.11 (cli) (built: Oct 28 2017 17:36:49) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.11, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
    with blackfire v1.18.0~linux-musl-x64-non_zts71, https://blackfire.io, by SensioLabs

Sonata packages:

sonata-project/admin-bundle              3.28.0    
sonata-project/block-bundle              3.9.0
sonata-project/cache                     2.0.1
sonata-project/core-bundle               3.7.1     
sonata-project/datagrid-bundle           2.3.1     
sonata-project/doctrine-orm-admin-bundle 3.3.0     
sonata-project/exporter                  1.8.0     

Symfony:

symfony/monolog-bundle                   v3.1.2  
symfony/phpunit-bridge                   v3.4.1  
symfony/polyfill-apcu                    v1.6.0  
symfony/polyfill-intl-icu                v1.6.0  
symfony/polyfill-mbstring                v1.6.0  
symfony/polyfill-php56                   v1.6.0  
symfony/polyfill-php70                   v1.6.0  
symfony/polyfill-util                    v1.6.0  
symfony/security-acl                     v3.0.0  
symfony/swiftmailer-bundle               v3.1.6  
symfony/symfony                          v3.4.1  

Doctrine:

doctrine/annotations                     v1.6.0   
doctrine/cache                           v1.7.1   
doctrine/collections                     v1.5.0   
doctrine/common                          v2.8.1   
doctrine/data-fixtures                   v1.3.0   
doctrine/dbal                            v2.6.3   
doctrine/doctrine-bundle                 1.8.1    
doctrine/doctrine-cache-bundle           1.3.2    
doctrine/doctrine-fixtures-bundle        v2.4.1   
doctrine/inflector                       v1.2.0   
doctrine/instantiator                    1.1.0    
doctrine/lexer                           v1.0.1   
doctrine/orm                             v2.5.13  

Any help would be greatly appreciated

jordisala1991 commented 6 years ago

Not sure if this is the same issue, was it working before with some version and not after an upgrade?

dmarkowicz commented 6 years ago

This is new issue related to #728. In #768 I fixed problem with composite keys. @rikvanderkemp reported another bug. ProxyQuery adds columns from OrderBy to Select to be SQL compliant. But if column in OrderBy is an association, it should be added to Select as IDENTITY(id).

Current DQL: SELECT DISTINCT m.id, m.root, m.lft, m.id FROM MenuBundle\Entity\Menu m ORDER BY m.root ASC, m.lft ASC, m.id ASC Valid DQL: SELECT DISTINCT m.id, IDENTITY(m.root) as root, m.lft, m.id FROM MenuBundle\Entity\Menu m ORDER BY m.root ASC, m.lft ASC, m.id ASC

For class Menu from case above:

$metadata = {Doctrine\ORM\Mapping\ClassMetadata} [40]
 name = "MenuBundle\Entity\Menu"
 identifier = {array} [1]
  0 = "id"
 fieldMappings = {array} [8]
  id = {array} [9]
  title = {array} [8]
  subtitle = {array} [8]
  url = {array} [8]
  active = {array} [9]
  lft = {array} [8]
  lvl = {array} [8]
  rgt = {array} [8]
 associationMappings = {array} [3]
  root = {array} [19]
  parent = {array} [19]
  children = {array} [16]

So, associationMappings in OrderBy should be detected and handled in different way.