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

时间:2017-02-27 07:40:10

标签: json typo3 extbase typo3-7.6.x

如何操作/更改返回的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
}]

例如:

{
"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"
}

我在我的控制器中尝试了这个,但它甚至没有过滤名称和&老板:

/**
 * @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());                    
}   

我仍然可以获得标准json中的所有字段。

这是来自存储库的功能:

public function jsonRequest() {

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

}

2 个答案:

答案 0 :(得分:1)

在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);
    }

}

请注意在控制器中生成整个$ repsonse,就像DataTables期望的那样(上面的最后几行)。这是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);
    }

}

答案 1 :(得分:0)

JsonView配置只能过滤/减少数据 - 这不能用于添加额外的计算属性,如初始问题中请求的那样。除此之外,关键字为_only而非only

您不需要使用json_encode(),仍然可以使用JsonView。但是,必须在控制器中单独计算数据有效负载 - 因此,包含例如recordsTotalDT_RowId属性。