TYPO3 Extbase - Manipulate/Change 返回 json
TYPO3 Extbase - Manipulate/Change returning json
我如何 manipulate/change 返回 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;
}
JsonView
配置仅支持 filtering/reducing 数据 - 这不能用于添加其他计算属性,如初始问题中所要求的那样。除此之外,关键字是 _only
而不是 only
.
您不需要使用 json_encode()
,仍然可以使用 JsonView
。然而,数据有效负载必须在您的控制器中单独计算 - 因此,包含例如recordsTotal
和 DT_RowId
属性。
在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);
}
}
我如何 manipulate/change 返回 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;
}
JsonView
配置仅支持 filtering/reducing 数据 - 这不能用于添加其他计算属性,如初始问题中所要求的那样。除此之外,关键字是 _only
而不是 only
.
您不需要使用 json_encode()
,仍然可以使用 JsonView
。然而,数据有效负载必须在您的控制器中单独计算 - 因此,包含例如recordsTotal
和 DT_RowId
属性。
在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);
}
}