我在尝试从不同于1的页面获取产品时遇到问题(有一个分页器)。如果有人能理解这个代码可能会很棒,我是symfony的新手。我认为这是由于这部分代码:
$this->get('request')->query->get('page', 1)
这是控制器。它将数据数据发送到名为getAll.html.twig.的页面
/**
* @Route("/get-all/{productRequestId}", name="userBuyerProductRequestGetAll", defaults={"productRequestId"=null} )
* @Template()
*/
public function getAllAction(Request $request, $productRequestId)
{
if ( !$this->getUser()->isAllowed("BUYER_PRODUCT_REQUEST")){
throw new AccessDeniedException();
}
if ( is_null($productRequestId) ) {
$productRequest = null;
} else {
$productRequest = $this->getRepo()->find($productRequestId);
}
$company = $this->getUser()->getCompany();
$criteria2 = $this->get('request')->query->get('criteria', "");
$pagination2 = $this->get('knp_paginator')->paginate(
$this->getDoctrine()->getRepository("dsarhoyaSCBundle:Product")->getByCompanyAndCriteriaDQL($company->getId(),$criteria2 ),
$this->get('request')->query->get('page', 1)/*page number*/,
$this->get('request')->query->get('resultsCount', 20)/*results per page*/,
array('defaultSortFieldName' => 'p.name', 'defaultSortDirection' => 'asc')
);
if ( !$this->getUser()->getCompany()->getProductRequestAccess() ){
return $this->redirectToRoute("userPurchaseBidIndex", array("companyId"=>$this->getUser()->getCompany()->getId()));
}
if ( !$this->getUser()->isAllowed("BUYER_PRODUCT_REQUEST")){
throw new AccessDeniedException();
}
$options = new ProductRequestOptions([
"criteria"=> $request->get("criteria",""),
"user"=>$this->getUser()
]);
$this->getDoctrine()->getManager()->getFilters()->disable('soft-deleteable');
$pagination = $this->get('knp_paginator')->paginate(
$this->getRepo()->getByCompanyDQL($options),
$this->get('request')->query->get('page', 1)/*page number*/,
$this->get('request')->query->get('resultsCount', 20)/*results per page*/,
array('defaultSortFieldName' => 'r.creationTime', 'defaultSortDirection' => 'desc')
);
return array(
"criteria"=> $options->criteria,
"requests"=>$pagination,
"company"=>$company,
"products"=>$pagination2,
"criteria"=>$criteria2,
"step"=>1,
"productRequest"=>$productRequest
);
}
您必须在URL中传递页面变量。现在,总会有一个"1"值,这是没有意义的。
您的路线/search-product/{productRequestId}
应至少包含一个页码。
作为您路线的一个元素:
/search-product/{productRequestId}/{page}
或作为获取参数:
/search-product/{productRequestId}/?page=number
其中number是您的页码。