简体   繁体   English

TYPO3 Extbase - 操作/更改返回的 json

[英]TYPO3 Extbase - Manipulate/Change returning json

How can I manipulate/change the returning json from:如何操作/更改返回的 json:

[{
    "name": "Audi",
    "owner": "Peter",
    "price": 0,
    "color": "Blue",
    "pid": 0,
    "uid": 1
}, {
    "name": "BMW",
    "owner": "Wolfgang",
    "price": 0,
    "color": "Black",
    "pid": 0,
    "uid": 2
}]

to eg:例如:

{
"data": [{
        "DT_RowId": "row_1",
        "name": "Audi",
        "owner": "Peter"
    }, {
        "DT_RowId": "row_2",
        "name": "BMW",
        "owner": "Wolfgang"
    }],
    "options": [],
    "files": [],
    "draw": 1,
    "recordsTotal": "2",
    "recordsFiltered": "16"
}

I tried this in my controller, but it's not even filtering for name & owner:我在我的控制器中尝试过这个,但它甚至没有过滤名称和所有者:

/**
 * @var string
 */
protected $defaultViewObjectName = 'TYPO3\CMS\Extbase\Mvc\View\JsonView';

public function jsonRequestAction() {           
    $this->view->setVariablesToRender(array('records'));                        
    $this->view->setConfiguration(array(
            'records' => array(
                'only' => array('name', 'owner')
            )   
        )
    );      
    $this->view->assign('records', $this->leiRepository->jsonRequest());                    
}   

I still get all the fields in the standard json.我仍然得到标准 json 中的所有字段。

That's the function from the repository:这是存储库中的功能:

public function jsonRequest() {

    $query = $this->createQuery();
    $result = $query->setLimit(1000)->execute();
    //$result = $query->execute();
    return $result;

}

Use your own JsonView in Controller:在控制器中使用您自己的 JsonView:

/**
 * JsonController
 */
class JsonController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

    /**
     * view
     * 
     * @var \TYPO3\CMS\Extbase\Mvc\View\JsonView
     */
    protected $view;

    /**
     * defaultViewObjectName
     * 
     * @var string
     */
    protected $defaultViewObjectName = \Vendor\Extension\View\JsonView::class;

    /**
     * action list
     *
     * @return void
     */
    public function listAction() {

        $response = [
            'draw' => $draw,
            'start' => $start,
            'length' => $length,
            'recordsTotal' => $allProducts->count(),
            'recordsFiltered' => $allProducts->count(),
            'order' => $order,
            'search' => $search,
            'columns' => $columns,
            'data' => $filteredProducts
        ];
        $this->view->setVariablesToRender(['response']);
        $this->view->assign('response', $response);
    }

}

Be aware to biuld the entire $repsonse in controller as like as the DataTables expects it (last lines above).请注意在控制器中构建整个 $repsonse,就像 DataTables 期望的那样(上面的最后几行)。 And here is the JsonView:这是 JsonView:

/**
 * JsonView
 */
class JsonView extends \TYPO3\CMS\Extbase\Mvc\View\JsonView {

    /**
     * @var array
     */
    protected $configuration = [
        'response' => [
            //'_only' => ['draw', 'start', 'length', 'order', 'columns', 'recordsTotal', 'recordsFiltered', 'data'],  
            'data' => [
                '_descend' => [
                    'product' => [
                        '_only' => ['uid'],
                    ],
                    'orderType' => [
                        '_only' => ['uid'],
                    ],
                ],
                '_descendAll' => [
                    //'_exclude' => ['title'],                      
                    '_descend' => [
                        'maturity' => [],
                        'currency' => [
                            '_only' => ['title'],
                        ],
                        'updated' => [],
                        'customer' => [
                            '_only' => ['title', 'title_short', 'titleShort'],
                        ],
                    ],
                ],                 
            ],
        ],        
    ];    

    /**
     * Always transforming ObjectStorages to Arrays for the JSON view
     *
     * @param mixed $value
     * @param array $configuration
     * @return array
     */
    protected function transformValue($value, array $configuration) {
        if ($value instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
            $value = $value->toArray();
        }
        return parent::transformValue($value, $configuration);
    }

}

The JsonView configuration is only capable of filtering/reducing data - this cannot be used to add additional computed properties like like requested in the initial question. JsonView配置只能过滤/减少数据 - 这不能用于添加额外的计算属性,如初始问题中请求的那样。 Besides that, the keyword is _only instead of only .除此之外,关键字是_only而不是only

You don't need to to use json_encode() and still can use JsonView .您不需要使用json_encode()并且仍然可以使用JsonView However the data payload has to be computed in your controller individually - thus, to contain eg recordsTotal and DT_RowId properties.然而,数据负载必须在您的控制器中单独计算 - 因此,包含recordsTotalDT_RowId属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM