我正在研究一个Symfony2项目,我决定使用KNPPaginatorBundle构建一个简单的分页系统.所以我创建了一个Product实体,我想将paginator添加到indexAction动作(由CRUD命令生成).
// Retrieving products.
$em = $this->getDoctrine()->getManager();
//$entities = $em->getRepository('LiveDataShopBundle:Product')->findAll();
$dql = "SELECT a FROM LiveDataShopBundle:Product a";
$entities = $em->createQuery($dql);
// Creating pagnination
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$entities,
$this->get('request')->query->get('page', 1),
20
);
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想使用产品的存储库,而不是直接在控制器中创建查询.我怎样才能做到这一点 ?实际上,直接将结果集合添加到paginate对象的速度太慢,因为它加载所有产品然后对ArrayCollection进行分页.
提前致谢.
K4
我在使用JMS Serializer排除某些KNP Paginator属性时遇到问题.
首先,它包含在composer.json中
...
"jms/serializer-bundle": "~0.13",
"knplabs/knp-paginator-bundle": "2.4.*@dev",
...
Run Code Online (Sandbox Code Playgroud)
我正在对CrmContacts实体进行分页,并且该实体的排除策略运行良好.我还为KNP Paginator添加了yml文件,如下所示:
config.yml
jms_serializer:
metadata:
directories:
KNPPB:
namespace_prefix: 'Knp\\Bundle\\PaginatorBundle'
path: %kernel.root_dir%/Resources/serializer/Knp
Run Code Online (Sandbox Code Playgroud)
在app/Resources/serializer/Knp文件夹中我创建了Pagination.SlidingPagination.yml:
Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination:
exclusion_policy: ALL
properties:
items:
expose: true
access_type: public_method
accessor:
getter: getItems
type: array
serialized_name:
payload
currentPageNumber:
expose: true
serialized_name:
page
numItemsPerPage:
expose: true
serialized_name:
items
totalCount:
expose: true
serialized_name:
totalItems
Run Code Online (Sandbox Code Playgroud)
这是返回序列化数据的逻辑:
public function getContactsAction(Request $request)
{
$limit = $request->query->getInt('l', 10);
$page = $request->query->getInt('p', 1);
$serializer = $this->get('jms_serializer');
$contacts = $this->getDoctrine()
->getManager()
->getRepository('AcmeContactsBundle:CrmContact')
->getContacts();
$paginator = $this->get('knp_paginator');
$pagination …
Run Code Online (Sandbox Code Playgroud) 我将我的项目从2.6升级到2.7,我在工作结束时收到此错误:
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
The service "knp_paginator.helper.processor" has a dependency on a non-existent service "templating.helper.router".
Run Code Online (Sandbox Code Playgroud)
我搜索服务容器,但我找不到服务列表中的服务(容器:调试),我在这个文件中找到了"templating.helper.router"的服务定义
/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml
Run Code Online (Sandbox Code Playgroud)
为什么这个服务不是由服务容器创建的?
/usr/bin/php /var/www/ghanbari/anar/app/console debug:container --tag=templating.helper -v
Deprecated: The Symfony\Component\DependencyInjection\Definition::setFactoryMethod method is deprecated since version 2.6 and will be removed in 3.0. Use Definition::setFactory() instead. in /var/www/ghanbari/anar/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Definition.php on line 137
Call Stack:
0.0004 227656 1. {main}() /var/www/ghanbari/anar/app/console:0
0.0161 2719288 2. Symfony\Component\Console\Application->run() /var/www/ghanbari/anar/app/console:27
0.0178 2896416 3. Symfony\Bundle\FrameworkBundle\Console\Application->doRun() /var/www/ghanbari/anar/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
0.0178 2896640 4. Symfony\Component\HttpKernel\Kernel->boot() /var/www/ghanbari/anar/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:70
0.0210 3132184 5. Symfony\Component\HttpKernel\Kernel->initializeContainer() /var/www/ghanbari/anar/app/bootstrap.php.cache:2387
0.0801 6683560 6. Symfony\Component\DependencyInjection\ContainerBuilder->compile() /var/www/ghanbari/anar/app/bootstrap.php.cache:2609
0.0806 6716880 7. Symfony\Component\DependencyInjection\Compiler\Compiler->compile() …
Run Code Online (Sandbox Code Playgroud)
我正在尝试将twitter bootstrap css样式应用到我的knp分页而不修改供应商.
有没有办法配置KnpPaginator以检测现有的bootstrap css样式表?因为截图显示,它是使用bootstrap构建的.
我正在使用knp paginator并且它运行良好但是当我想使用它的排序功能时,我有问题在树枝中获得排序方向.
以下代码指示如何获取已排序的表头但未考虑如何获取已排序的表头方向.
{# total items count #}
<div class="count">
{{ pagination.getTotalItemCount }}
</div>
<table>
<tr>
{# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th>
<th{% if pagination.isSorted('a.Title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'a.title') }}</th>
</tr>
{# table body #}
{% for article in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ article.id }}</td>
<td>{{ article.title }}</td>
</tr>
{% endfor %}
</table>
{# display navigation #}
<div class="navigation">
{{ knp_pagination_render(pagination) …
Run Code Online (Sandbox Code Playgroud) 我有问题,knp paginator只能这样工作:
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM MainArtBundle:Art a";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1) /*page number*/,
8 /*limit per page*/
);
Run Code Online (Sandbox Code Playgroud)
但不是这样:
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('MainArtBundle:Art')->findAll();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$entities,
$this->get('request')->query->get('page', 1) /*page number*/,
/*limit per page*/
);
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我不明白.
这是我的树枝电话:
<li>{{ knp_pagination_sortable(paginator, 'Oldest', 'a.id', {'direction': 'desc'}) }}</li>
Run Code Online (Sandbox Code Playgroud)
问候迈克尔
我在Symfony2项目中使用KnpPaginatorBundle。当我尝试将Doctrine 2本机查询传递给分页程序实例时,出现错误:
One of listeners must count and slice given target
Run Code Online (Sandbox Code Playgroud)
有没有人为某些本机查询提供此示例的正确实现示例?
在bundle的文档中,我看到了示例(https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/doc/custom_pagination_subscribers.md),但仅适用于文件系统,我不知道如何将其转换为db查询。
你能帮我吗?
编辑
我的查询:
SELECT a.*, highest_rated_book.*
FROM authors a
LEFT JOIN (SELECT * FROM books b ORDER BY b.rate DESC) AS highest_rated_book
ON a.id = highest_rated_book.author_id
GROUP BY highest_rated_book.author_id
ORDER BY a.id;
Run Code Online (Sandbox Code Playgroud)
和表:
author (id, first_name, last_name)
books (id, title, rate, author_id)
Run Code Online (Sandbox Code Playgroud) 它如何呈现它
1 2 3 > >> (page numbers, next button, last page button)
Run Code Online (Sandbox Code Playgroud)
我需要它如何呈现
> (only next button)
Run Code Online (Sandbox Code Playgroud)
如何在twig文件上触发render方法
<div class="pagination">
{{ knp_pagination_render(pagination) }}
</div>
Run Code Online (Sandbox Code Playgroud)
也许这会有所帮助,这是knp paginator源代码中的render函数
/**
* Renders the pagination template
*
* @param string $template
* @param array $queryParams
* @param array $viewParams
*
* @return string
*/
public function render($pagination, $template = null, array $queryParams = array(), array $viewParams = array())
{
return $this->environment->render(
$template ?: $pagination->getTemplate(),
$this->processor->render($pagination, $queryParams, $viewParams)
);
}
/**
* Get name …
Run Code Online (Sandbox Code Playgroud) 我有一个巨大的产品表(100k +行),在我的控制器中我有以下功能:
public function indexAction(Request $request)
{
$findProducts = $this->getDoctrine()
->getRepository("StockBundle:Product")->findAll();
$paginator = $this->get('knp_paginator');
$producten = $paginator->paginate(
$findProducts,
$request->query->getInt('page', 1)/*page number*/,
20/*limit per page*/
);
return $this->render('StockBundle:Default:index.html.twig',
array('producten' => $producten));
}
Run Code Online (Sandbox Code Playgroud)
问题是页面加载大约需要11-12秒,占用233MB的RAM.
我该怎么做才能提高速度并减少内存?
这是我的实体:
/**
* Product
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Namespace\StockBundle\Entity\ProductRepository")
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="naam_nl", type="string", length=255)
*/
private $naamNl;
/**
* @var string
*
* @ORM\Column(name="naam_fr", …
Run Code Online (Sandbox Code Playgroud) 我试图获取当前在knp paginator包中显示的项目的当前索引,atm我有这样的事情:
{% for index, post in posts %}
{{ index + 1 }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
然而,这仅显示例如1 - 10,即使我在第2页上.它应该显示页面上的项目11-20,第3页上的21-30,...但它每次都会重置.
我用以下方法解决了它:
{{ pagination.getItemNumberPerPage * (pagination.getCurrentPageNumber - 1) + index + 1 }}
Run Code Online (Sandbox Code Playgroud)
但这是一个非常混乱的解决方案,我不想使用.
这有什么替代方案吗?
knppaginator ×10
symfony ×9
pagination ×4
php ×4
doctrine-orm ×3
twig ×3
bundle ×1
nativequery ×1
symfony-2.7 ×1