lch / components-bundle
所有 LCH Bundle 的通用组件
1.2.8
2020-06-23 13:08 UTC
Requires
- php: ^7.2
- lch/translate-bundle: 0.5.3
- ramsey/uuid-doctrine: ^1.5
- symfony/framework-bundle: ^4.4
- symfony/translation: ^4.4
Requires (Dev)
- phpunit/phpunit: ~4.8|~5.0
README
这个 Symfony Bundle 提供了跨其他 LCH Bundle 所需的所有必需的砖块。
安装
composer require lch/components-bundle "^1.2.7"
特质
UUID
使用 Uuidable
将在您的实体中添加一个 Ramsey UUID 身份字段。 注意:这不会为您处理 配置步骤。
仓库特质
为了将一些行为集中在同一个地方,我们引入以下仓库特质。
PaginableEntityRepository
这提供了一个公共方法来检索一个 Doctrine Paginator
对象。以下是其签名
/** * @param QueryBuilder $qb the QueryBuilder object containing the query description * @param int $page the page wanted (set to 1 by default) * @param int|null $maxResults the batch size, if any * * @return Paginator */ public function getPaginator(QueryBuilder $qb, int $page = 1, int $maxResults = null)
这里的一切都很简单。
CountableEntityRepository
这提供了一个简单的方法,可以轻松获取总查询项数。
SearchableEntityRepository
全文搜索方法
这在 AJAX 动作(在非 API 上下文中)非常有用,例如在需要在大范围上下文中检索实体列表的情况下:经典的 AJAX 搜索。
/** * Used to search in like mode in entity fields * @param array $fields the fields names wanted to be searched in * @param string $term the term to be searched * @param int|null $maxResults the batch size, if any. * @param string|null $language if any, ISO code language to filter items on * * @return array */ public function findByFulltextTerm( array $fields, string $term, int $maxResults = null, string $language = null ): array
Twig
宏
简单的管理员分页
如果您使用上述 PaginableEntityRepository
,则可以传递分页对象
public function list(int $page): Response { // get items before with a repository method returning paginator return $this->render('admin/menu/list.html.twig', [ 'menus' => $menus, 'pagination' => [ 'page' => $page, 'nbPages' => ceil($menus->count() / $nbItemsPerPage) ] ]); }
然后在 twig 中可以生成简单的分页组件
{% import "@LchComponents/macros/lch-components-utils.html.twig" as lch_utils %} ... {{ lch_utils.admin_paginate(pagination.page, pagination.nbPages) }}