简体   繁体   English

将自定义查询添加到Symfony2中的分页器(KNP Paginator)?

[英]Adding custom query to paginator (KNP Paginator) in Symfony2?

I have the following query , how to write these query in Symfony2 我有以下查询,如何在Symfony2中编写这些查询

SELECT Inventory_Stock.id, Inventory_Stock.quantity, SUM(InventoryUsage.quantity)
       ,Inventory_Stock.quantity - SUM(InventoryUsage.quantity) AS Stock 

FROM Inventory_Stock LEFT JOIN InventoryUsage ON Inventory_Stock.id = InventoryUsage.InventoryStock_id 

WHERE Inventory_Stock.id = 26 OR
      Inventory_Stock.id = 27

GROUP BY Inventory_Stock.id 
ORDER BY Stock DESC

i need to implement the above query in a symfony way 我需要以symfony的方式实现上述查询

 private function getList($query = null)
{
    $em = $this->getDoctrine()->getEntityManager();

    if(!$query)
    {
        $query = $em->createQueryBuilder()
            ->select('i')
            ->from('ItxBundle:InventoryStock', 'i')
            ->innerJoin('i.Product','p')
            ->getQuery();

    }
    $adapter = $this->get('knp_paginator.adapter');
    $adapter->setQuery($query);
    $adapter->setDistinct(TRUE);


    $paginator = new Paginator($adapter);
    $paginator->setCurrentPageNumber($this->get('request')->query->get('page', 1));
    $paginator->setItemCountPerPage($this->container->parameters['items_per_page']);
    $paginator->setPageRange($this->container->parameters['page_range']);
    return $paginator;
}

and in the view 并认为

{% if entity.quantity - entity.Usage < 0 %}
    0
{% else %}
    {{ entity.quantity - entity.Usage | number_format(0) }}
{% endif %}

I am using three tables here , relation given below 我在这里使用三个表,关系如下

InventoryStock 1 - n InventoryUsage InventoryStock 1 - 1 Product 库存库存1-n库存用法库存库存1-1产品

need to show the available stock (InventoryStock.Quanitity - sum(InventoryUsage.quanitity)) 需要显示可用库存(InventoryStock.Quanitity-sum(InventoryUsage.quanitity))

also need to implement sort based on Stock as well 还需要实现基于库存的排序

Would be great if any one could help me out as this has been pulling my hair out for couple of days. 如果有人可以帮助我,那会很棒,因为这已经将我的头发拉了几天。

I'd recommend using the WhiteOctoberPagerfantaBundle bundle for this. 我建议为此使用WhiteOctoberPagerfantaBundle捆绑包。 It allows you to create your own Query object as you normally would, and then pass this to the relevant Pagerfanta adapter. 它使您可以像平常一样创建自己的Query对象,然后将其传递给相关的Pagerfanta适配器。 It looks as if you're using Doctrine, so for example... 看起来好像您正在使用Doctrine,因此例如...

    // at the top of the file:
    use Pagerfanta\Pagerfanta, 
        Pagerfanta\Adapter\DoctrineORMAdapter;


    // Build your query...
    /** @var $query \Doctrine\ORM\Query */
    //$query = ... 

    $currentPage = $this->getRequest()->get('page', 1);

    $pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
    $pagerfanta
        ->setMaxPerPage($maxItems) // you'll need to specify this value
        ->setCurrentPage($currentPage)
    ;
    $results = $pagerfanta->getCurrentPageResults();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM