Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderByWalker.php on line 57 - there is no $meta['metadata'] #535

Open
ArantecEnginheria opened this issue Feb 18, 2019 · 2 comments
Open

Comments

@ArantecEnginheria
Copy link

ArantecEnginheria commented Feb 18, 2019

Hi!

I got that error when sorting the table:

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Call to a member function hasField() on null" at /vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/Query/OrderByWalker.php line 57 {"exception":"[object] (Symfony\Component\Debug\Exception\FatalErrorException(code: 0): Error: Call to a member function hasField() on null at /vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/Query/OrderByWalker.php:57)"} []

The first page shows as expected, click on page index works too, but the problem comes when ordering by any criteria (sector.id/sector.name/sector.client.name). Debugging I realize that $meta['metadata'] is not set, and therefore hasField($field) crashes.

Controller:

public function indexAction() {
    $em = $this->getDoctrine()->getManager();

    $dql = "SELECT s as sector, COUNT(e.station) AS count_stations
            FROM SectorsBundle:Sector s
            LEFT JOIN s.stations e
            GROUP BY e.sector";
        
    $query = $em->createQuery($dql);
               
    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query, $this->get('request')->query->getInt('page', 1), $this->container->getParameter('num_elements')
    );

    return $this->render('SectorsBundle:Backend:indexSector.html.twig', array(
        'pagination' => $pagination,
    ));
}

Frontend twig file:

<table class="tablesorter" cellspacing="0"> 
    <thead> 
        <tr> 
           <th>{{ knp_pagination_sortable(pagination, ('sector.id' | trans), 'sector.id')|raw }}</th> 
           <th>{{ knp_pagination_sortable(pagination, ('sector.nom' | trans), 'sector.name')|raw }}</th> 
           <th>{{ knp_pagination_sortable(pagination, ('sector.client' | trans), 'sector.client.name')|raw }}</th> 
           <th>{{'sector.estacions' | trans}}</th> 
           <th>&nbsp;</th> 
       </tr> 
   </thead> 
    <tbody> 
            {% for entity in pagination %}
                <tr> 
                    <td>{{ entity.sector.id }}</td> 
                    <td>a href="{{ path('backend_sector_edit', { 'id': entity.sector.id }) }}">{{ entity.sector.name }}</a></td> 
                    <td>a href="{{ path('backend_sector_edit', { 'id': entity.sector.id }) }}">{{ entity.sector.client.nom }}</a></td> 
                    <td>a href="{{ path('backend_station_by_sector', { 'id': entity.sector.id }) }}">{{'sector.see.stations'| trans({'%count%': entity.count_stations})}}</a></td>
                    <td>
                        <ul>
                            <li>
                                <a href="{{ path('backend_sector_edit', { 'id': entity.sector.id }) }}"><input type="image" src="{{ asset('backend/images/icn_edit.png') }}" title="Edit"></a>
                            </li>
                            <li>
                                <a href="{{ path('backend_sector_delete', { 'id': entity.sector.id }) }}"><input type="image" class="JS-delete" src="{{ asset('backend/images/icn_trash.png') }}" title="Delete"></a>
                            </li>
                        </ul>
                    </td>
                </tr> 
            {% endfor %}
    </tbody> 
</table>

Parameters:

knp_paginator.template.pagination: BackendBundle:Pagination:sliding.html.twig
  knp_paginator.template.sortable:   BackendBundle:Pagination:sortable_link.html.twig
  knp_paginator.default_options.page_name: page                 
  knp_paginator.default_options.sort_field_name: sort           
  knp_paginator.default_options.sort_direction_name: direction  
  knp_paginator.default_options.distinct: true                  
  knp_paginator.default_options.filter_field_name: filterField  
  knp_paginator.default_options.filter_value_name: filterValue  

Any suggestion/hint about whats wrong?

Thank you!

@ArantecEnginheria
Copy link
Author

ArantecEnginheria commented Feb 19, 2019

Composer, just in case need to know...

{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
            "psr-0": { "": "src/" }
    },
    "classmap": [ 
         "app/AppKernel.php", "app/AppCache.php"],
    "require": {
        "symfony/symfony": " 2.8.41",
        "doctrine/orm": " >=2.2.3,<2.5-dev",
        "doctrine/doctrine-bundle": "^1.10",
        "twig/extensions": "^1.5",
        "symfony/assetic-bundle": "^2.8",
        "symfony/swiftmailer-bundle": "2.2.*",
        "symfony/monolog-bundle": "^3.3",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "3.*",
        "sensio/generator-bundle": "^3.1",
        "jms/security-extra-bundle": "^1.6",
        "jms/di-extra-bundle": "^1.9",
        "phpoffice/phpexcel": "^1.8",
        "jms/metadata": "^1.7",
        "friendsofsymfony/jsrouting-bundle": "^2.3",
        "stfalcon/tinymce-bundle": "2.*",
        "knplabs/knp-paginator-bundle": "^2.8",
        "symfony/security-guard": "2.*",
        "jms/serializer-bundle": "^2.4",
        "nelmio/api-doc-bundle": "^2.13",
        "aws/aws-sdk-php-symfony": "^2.0"
    },
    "scripts": {
        "post-install-cmd": [
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ]
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "branch-alias": {
            "dev-master": "2.1-dev"
        }
    }
}

@A-Obukhov
Copy link

You should use Your alias in knp_pagination_sortable():

<th>{{ knp_pagination_sortable(pagination, ('sector.id' | trans), 's.id')|raw }}</th> 
<th>{{ knp_pagination_sortable(pagination, ('sector.nom' | trans), 's.name')|raw }}</th> 

Also, s.client.name is probably relation, so You need yet another JOIN:

LEFT JOIN s.client c

After that you can sort by client name such way:

<th>{{ knp_pagination_sortable(pagination, ('sector.client' | trans), 'c.name')|raw }}</th> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants