簡體   English   中英

Symfony3 Datatable無法呈現

[英]Symfony3 Datatable unable to render

在MySQL數據庫中,我正在為IPv4 / IPv6地址存儲VARBINARY(16)。 為了在表單上顯示它,我創建了一個DataTransformer,以便使用內置的inet_ptoninet_ntop函數。

我想在Datatable中顯示它,因此可以通過IP地址進行搜索。 不幸的是,通過使用我的典型數據表設置,我得到500個服務器錯誤。

通過使用瀏覽器開發人員工具,我能夠確定AJAX調用的結果是獲取部分數據,實際的IP地址是作為資源而不是真實數據列出的。

錯誤說明: 無法規范化意外值:NULL

在堆棧跟蹤中,我看到:

at Serializer ->normalize (resource, 'json', array()) 
in vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php at line 152

at Serializer ->normalize (array('id' => '5', 'ipAddress' => resource, 'sg_datatables_editable' => array(false), 'sg_datatables_actions' => array(array('network_ipaddress_show' => true))), 'json', array())
in vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php at line 152

at Serializer ->normalize (array(array('id' => '5', 'ipAddress' => resource, 'sg_datatables_editable' => array(false), 'sg_datatables_actions' => array(array('network_ipaddress_show' => true))), array('id' => '6', 'ipAddress' => resource, 'sg_datatables_editable' => array(false), 'sg_datatables_actions' => array(array('network_ipaddress_show' => true)))), 'json', array()) 
in vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php at line 152

at Serializer ->normalize (array('draw' => '1', 'recordsTotal' => '2', 'recordsFiltered' => '2', 'data' => array(array('id' => '5', 'ipAddress' => resource, 'sg_datatables_editable' => array(false), 'sg_datatables_actions' => array(array('network_ipaddress_show' => true))), array('id' => '6', 'ipAddress' => resource, 'sg_datatables_editable' => array(false), 'sg_datatables_actions' => array(array('network_ipaddress_show' => true))))), 'json', array()) 
in vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php at line 115

at Serializer ->serialize (array('draw' => '1', 'recordsTotal' => '2', 'recordsFiltered' => '2', 'data' => array(array('id' => '5', 'ipAddress' => resource, 'sg_datatables_editable' => array(false), 'sg_datatables_actions' => array(array('network_ipaddress_show' => true))), array('id' => '6', 'ipAddress' => resource, 'sg_datatables_editable' => array(false), 'sg_datatables_actions' => array(array('network_ipaddress_show' => true))))), 'json') 
in vendor/sg/datatablesbundle/Sg/DatatablesBundle/Datatable/Data/DatatableQuery.php at line 743

看到JSON調用,我實現了JsonSerializable接口,但它似乎沒有被這樣拉動。

雖然可以選擇將inet_pton或inet_ntop直接放在實體中,但我相信我會失去驗證。

我可以實現哪些接口以允許Datatables拉取我的IPAddress實體,並正確顯示IP地址?

請注意,數據不會從實體(VARBINARY)轉換為數據表所需的可讀字符串。 堆棧跟蹤顯示它是“資源”。

新增代碼:

我的實體:

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;

/**
 * @ORM\Table(name="ip_address")
 */
class IPAddress implements JsonSerializable
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var mixed
     * @ORM\Column(name="ip_address", type="binary", length=16, nullable=false, unique=true)
     */
    private $ipAddress;

    /**
     * Set IP Address
     * @param mixed $ipAddress
     * @return IPAddress
     */
    public function setIpAddress($ipAddress)
    {
        $this->ipAddress = $ipAddress;
        return $this;
    }
    /**
     * Get IP Address
     * @return mixed
     */
    public function getIpAddress()
    {
        return $this->ipAddress;
    }

    public function jsonSerialize()
    {
        return array(
            'ipAddress' => inet_pton($this->ipAddress),
        );
    }

Datatable類:

<?php

namespace AppBundle\Datatables;
use Sg\DatatablesBundle\Datatable\View\AbstractDatatableView;
use Sg\DatatablesBundle\Datatable\View\Style;

/**
 * Class IPAddressDatatable
 * @package AppBundle\Datatables
 */
class IPAddressDatatable extends AbstractDatatableView
{
    /**
     * {@inheritdoc}
     */
    public function buildDatatable(array $options = array())
    {
        $this->features->set(array(
            'auto_width' => true,
            'defer_render' => false,
            ... other default features not listed ...
            'delay' => 0,
            'extensions' => array()
        ));

        $this->ajax->set(array(
            'url' => $this->router->generate('network_ipaddress_results'),
            'type' => 'GET'
        ));

        $this->options->set(array(
            'display_start' => 0,
            'defer_loading' => -1,
            ... other default options not listed ...
            'use_integration_options' => false,
            'force_dom' => false
        ));

        $this->columnBuilder
            ->add('ipAddress', 'column', array(
                'title' => 'IP Address',
                'width' => '85%',
            ))
            ->add(null, 'action', array(
                'title' => '',
                'width' => '15%',
                'actions' => array(
                    array(
                        'route' => 'network_ipaddress_show',
                        'route_parameters' => array(
                            'plan_id' => 'id'
                        ),
                        'label' => $this->translator->trans('datatables.actions.show'),
                        'icon' => 'fi-eye icon-size-14',
                        'attributes' => array(
                            'rel' => 'tooltip',
                            'title' => $this->translator->trans('datatables.actions.show'),
                            'class' => 'tiny button',
                            'role' => 'button'
                        ),
                    ),
                ),
            ));
    ;
    }

    ... getEntity and getName not shown for brevity ...
}

財務主任(至少相關部分):

/**
 * Lists all IP Addresses.
 *
 * @Route("/", name="network_ipaddress_index")
 * @Method("GET")
 * @return IPAddressController
 */
public function indexAction()
{
    $datatable = $this->get('app.datatable.ipaddress');
    $datatable->buildDatatable();

    return $this->render(':network:ipaddress_datatable.html.twig', array(
        'datatable' => $datatable,
    ));
}

/**
 * Returns a response, only used for datatables
 *
 * @Route("/results", name="network_ipaddress_results")
 * @Method("GET")
 * @return IPAddressController
 */
public function indexResultsAction()
{
    $datatable = $this->get('app.datatable.ipaddress');
    $datatable->buildDatatable();

    // I believe that the entity data would have to be translated here into printable a format

    $query = $this->get('sg_datatables.query')->getQueryFrom($datatable);

    return $query->getResponse();
}

事實證明, stwe / DatatablesBundle中有一個可用的預渲染閉包。

那么,上面的Datatables類所需的函數:

/**
 * {@inheritdoc}
 */
public function getLineFormatter()
{
    $formatter = function($line){
        $str = stream_get_contents($line['ipAddress']);
        if( strlen( $str ) == 16 OR strlen( $str ) == 4 ){
            $line['ipAddress'] = inet_ntop( pack( "A".strlen( $str ) , $str ) );
        }
        return $line;
    };
    return $formatter;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM