gpt4 book ai didi

php - 排序在 Knp 分页器中不起作用

转载 作者:可可西里 更新时间:2023-10-31 23:19:38 25 4
gpt4 key购买 nike

我正在使用 knp paginator 包,但出现此错误 There is no such field [catalogId] in the given Query component, aliased by [u] 。如果我点击标题,排序工作正常,但如果我点击 catalogid,它会显示错误。CatalogId 是 ManytoOne 关系。我用谷歌搜索了答案,但似乎对我没有任何帮助。你能告诉我如何解决这个问题吗? ?

这是我的 Controller :

public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$postData = $request->query->all();

$form = $this->createForm(new SkuInventoryType(), new SkuInventory());
$form->handleRequest($request);
if($postData){
$repo = $this->getDoctrine()->getRepository('RetailMappingCatalogBundle:SkuInventory');
//dump($repo);die;
$skuQuery = $repo->createQueryBuilder('u')
->orderBy($postData['sort'],$postData['direction']);
}else{
$skuQuery = $em->getRepository('RetailMappingCatalogBundle:SkuInventory:u')->findAll();
}
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$skuQuery,
$request->query->get('page', 1),
15
);
if ($form->isSubmitted() && $form->isValid())
{
$data = $form->getData();
$data->setCreatedBy($this->getUser());
$data->setUpdatedBy($this->getUser());

$em->persist($data);
$em->flush();
$alertMessage = $this->get('retail_mapping.alert_message');
$alertMessage->success('SKU Inventory Created');

return $this->redirect($this->generateUrl('sku_inventory'));
}

return $this->render('User/SkuInventory/index.html.twig',[
'form' => $form->createView(),
'pagination' => $pagination,
]);
}

这是我的观点:

<table class="table table-bordered table-striped">
<tr>
<th>Id</th>

<th{% if pagination.isSorted('u.title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'u.title') }}</th>
<th{% if pagination.isSorted('u.catalogId') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'SKU', 'u.catalogId') }}</th>
<th>Actions</th>
</tr>

{% for sku in pagination %}

<tr>
<td>{{ loop.index }}</td>
<td>{{ sku.title}}</td>
<td> {{sku.catalogId.title}}</td>
<td><a class="btn btn-primary" href="{{path('sku_inventory_edit',{'id': sku.id})}}">Edit</a>&nbsp;&nbsp;
<a class="btn btn-primary" href="{{path('sku_inventory_delete',{'id': sku.id})}}">Delete</a></td>
</tr>
{% endfor %}

</table>

<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>

最佳答案

经过一番摸索,我终于找到了答案。这是我的 Controller

public function indexAction(Request $request)
{
$breadcrumbs = $this->get("white_october_breadcrumbs");
$breadcrumbs->addItem("Sku Inventory", $this->get("router")->generate("index"));
$em = $this->getDoctrine()->getManager();

$form = $this->createForm(new SkuInventoryType(), new SkuInventory());
$form->handleRequest($request);

$repo = $em->getRepository('RetailMappingCatalogBundle:SkuInventory')->findAllCatalog();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$repo,
$request->query->get('page', 1),
15
);
if ($form->isSubmitted() && $form->isValid())
{
$data = $form->getData();
$data->setCreatedBy($this->getUser());
$data->setUpdatedBy($this->getUser());

$em->persist($data);
$em->flush();
$alertMessage = $this->get('retail_mapping.alert_message');
$alertMessage->success('SKU Inventory Created');

return $this->redirect($this->generateUrl('sku_inventory'));
}

return $this->render('User/SkuInventory/index.html.twig',[
'form' => $form->createView(),
'pagination' => $pagination,
]);
}

这是我的仓库:

public function findAllCatalog()
{
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select('c')
->from('RetailMappingCatalogBundle:SkuInventory', 'c')
->Join('c.catalogId', 'cl')
->orderBy('c.id', 'DESC');
//dump($qb->getQuery());die;
return $qb->getQuery();
}

这是我的观点:

<table class="table table-bordered table-striped">
<tr>
<th>Id</th>

<th{% if pagination.isSorted('c.title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'c.title') }}</th>
<th{% if pagination.isSorted('cl.catalogId.title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'SKU', 'cl.catalogId.title') }}</th>
<th>Actions</th>
</tr>

{% for sku in pagination %}

<tr>
<td>{{ loop.index }}</td>
<td>{{ sku.title}}</td>
<td> {{sku.catalogId.title}}</td>
{{ dump(sku) }}
{#}<td> {{sku.catalogId.title}}</td>{#}
<td><a class="btn btn-primary" href="{{path('sku_inventory_edit',{'id': sku.id})}}">Edit</a>&nbsp;&nbsp;
<a class="btn btn-primary" href="{{path('sku_inventory_delete',{'id': sku.id})}}">Delete</a></td>
</tr>
{% endfor %}

</table>

<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>

关于php - 排序在 Knp 分页器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29120352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com