0

I want to sort a list in Sonata Admin which contains IP-Addresses (Version 4) (other contains IPv6). Is there any change to sort it?

Current sort:

  • 172.24.0.1
  • 172.24.0.200
  • 172.24.0.4
  • 172.24.0.5

Correct order:

  • 172.24.0.1
  • 172.24.0.4
  • 172.24.0.5
  • 172.24.0.200

The behavior should be correct when I click on the header. IP-Sorting is not the default-orderBy setting.

EDIT

I have realized a (D2-)function which calculates to INT-value for a IP-Address.

db2 "select INET_ATON (device_ip_v4) from devices order by INET_ATON (device_ip_v4)";                                                                                                                           
1
--------------------
        16843009
        16843009
        16843009
      2887254017

Device-Entity:

<?php

/**
 * @ORM\Entity(repositoryClass="MonngBundle\Repository\DevicesRepository")
 * @ORM\Table(name="devices")
 */
class Devices
{

/**
     * @var integer
     *
     * @ORM\Column(name="device_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

   /**
     * @var string
     *
     * @ORM\Column(name="device_name", type="string", length=255, nullable=true, options={"default": ""})
     */
    protected $deviceName = '';

   /**
     * @var string
     * @Assert\Ip(version="4")
     * @ORM\Column(name="device_ip_v4", type="string", length=20, nullable=true)
     */
    protected $deviceIpV4;

     ...

Now I want my Sonata Admin to sort IP using this function

<?php

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Route\RouteCollection;

    class DevicesAdmin extends AdminClass
    {
        protected $datagridValues = array(

            '_sort_by' => 'INET_ATON(device_ip_v4)',
        );

    /**
         * @param DatagridMapper $datagridMapper
         */
        protected function configureDatagridFilters(DatagridMapper $datagridMapper)
        {
            $user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();

        parent::configureDatagridFilters($datagridMapper)
            ->add('id', null, array('label' => 'Scan-ID'))
            ->add('deviceName', null, array('label' => 'Device-Name'))
            ->add('deviceIpV4', null, array('show_filter' => true, 'label' => 'IP-Address (V4)', 'sortable' => 'INET_ATON(deviceIpV4)' ))

But I just get:

ProxyQuery {#11119 ▼
  #queryBuilder: QueryBuilder {#11120 ▶}
  #sortBy: null
  #sortOrder: null
  #uniqueParameterId: 0
  #entityJoinAliases: []
}
Atreides78
  • 577
  • 1
  • 7
  • 22

0 Answers0