Writing raw query in symfony2 and passing it to pa

2019-08-23 07:26发布

问题:

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 Asc

How can I write the above code in Symfony2, I want to write it as raw query

Also I am using PageFanta for pagination.. so the result from the above query will go to pagination.

Relations are :

Product ( 1 - 1) InventoryStock InventoryStock ( 1 - n ) InventoryUsage

回答1:

If it isn't a problem get all records to array. And use ArrayAdapter:

$adapter = new ArrayAdapter($array);
$pagerfanta = new Pagerfanta($adapter);

If the count of the records is too big. Write your own adapter for pagerfanta