TYPO3 Extbase - 通过大桌子分页(100000条记录)

时间:2017-03-03 16:59:33

标签: typo3 extbase typo3-7.6.x

我有一个相当大的表,大约有100000条记录。如果我没有在存储库中设置限制

存储库:

public function paginateRequest() {

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

}

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

$this->view->assign('records', $this->leiRepository->paginateRequest());

//$this->view->assign('records', $this->leiRepository->findAll());

}

...查询和分页虽然我使用f:widget.paginate。根据文档https://fluidtypo3.org/viewhelpers/fluid/master/Widget/PaginateViewHelper.html,我希望我只能渲染itemsPerPage和' parginate'通过记录...

List.hmtl

<f:if condition="{records}">
<f:widget.paginate objects="{records}" as="paginatedRecords" configuration="{itemsPerPage: 100, insertAbove: 0, insertBelow: 1, maximumNumberOfLinks: 10}">                     
    <f:for each="{paginatedRecords}" as="record">
        <tr>
            <td><f:link.action action="show" pageUid="43" arguments="{record:record}"> {record.name}</f:link.action></td>
            <td><f:link.action action="show" pageUid="43" arguments="{record:record}"> {record.lei}</f:link.action></td>
        </tr>
    </f:for>
</f:widget.paginate>

型号:

类雷扩展\ TYPO3 \ CMS \ Extbase \ DomainObject \ AbstractEntity {

...
/**
 * lei
 *
 * @lazy
 * @var string
 */
protected $lei = '';
...

3 个答案:

答案 0 :(得分:1)

要分页的对象有多大/多复杂?如果它们在列表视图中有您不需要的子对象,请将@lazy注释添加到模型内的那些关系中。

由于记录量很大,您应该在列表视图中尽可能简单。您可以尝试仅使用$this->leiRepository->findAll()->toArray()将结果作为数组提供给列表视图,或者通过向execute(true)添加true来仅返回存储库中的原始结果。

您还可以在控制器中的foreach中自己创建一个列表项数组,并仅在列表中添加您真正需要的属性。

答案 1 :(得分:1)

如果你的问题是性能,只需使用默认的findAll() - Method。

\ TYPO3 \ CMS \ Extbase \ Persistence \ Repository中的内置defaultQuerySettings根据分页小部件设置其偏移和限制,否则设置。

如果性能问题仍然存在,您可能必须考虑为数据库请求编写自定义查询,该查询仅请求视图实际显示的数据。该过程在文档中进行了描述:https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html

答案 2 :(得分:1)

我在TYPO3 9.5中使用。存储库中的下一个功能:

public function paginated($page = 1, $size = 9){
    $query = $this->createQuery();
    $begin = ($page-1) * $size;
    $query->setOffset($begin);
    $query->setLimit($size);
    return $query->execute();
}

在控制器中,我使用参数作为参数来发送页面以在Rest操作中加载。

public function listRestAction()
{

    $arguments = $this->request->getArguments();
    $totalElements = $this->repository->total();
    $pages = ceil($totalElements/9);
    $next_page = '';
    $prev_page = '';
    #GET Page to load
    if($arguments['page'] AND $arguments['page'] != ''){
        $page_to_load = $arguments['page'];
    } else {
        $page_to_load = 1; 
    }
    #Configuration of pagination
    if($page_to_load == $pages){
        $prev = $page_to_load - 1;
        $prev_page = "http://example.com/rest/news/page/$prev";
    } elseif($page_to_load == 1){
        $next = $page_to_load + 1;
        $next_page = "http://example.com/rest/news/page/$next";
    } else {
        $prev = $page_to_load - 1;
        $prev_page = "http://example.com/rest/news/page/$prev";
        $next = $page_to_load + 1;
        $next_page = "http://example.com/rest/news/page/$next";
    }

    $jsonPreparedElements = array();
    $jsonPreparedElements['info']['count'] = $totalElements;        
    $jsonPreparedElements['info']['pages'] = $pages;
    $jsonPreparedElements['info']['next'] = $next_page;
    $jsonPreparedElements['info']['prev'] = $prev_page;
    $result = $this->repository->paginated($page_to_load);
    $collection_parsed_results = array();
    foreach ($result as $news) {
        array_push($collection_parsed_results, $news->parsedArray());
    }
    $jsonPreparedElements['results'] = $collection_parsed_results;
    $this->view->assign('value', $jsonPreparedElements);
}

结果是这样的JSON:

{
"info": {
    "count": 25,
    "pages": 3,
    "next": "",
    "prev": "http://example.com/rest/news/page/2"
},
"results": [
    { ....}
] }