Passing an id get from a method, to another method inside same controller or template - Symfony

Viewed 27

I'm stuck in a problem. I have two differents repository, and i want use two differents methods that one need an id get from the other method.

First repository is OrdersRepository.php, where i have the function :

    public function getFilteredOrders()
{
    $conn = $this->getEntityManager()->getConnection();
    $sql = '
    SELECT o.id_address_delivery,o.id_address_invoice,o.date, o.en_reference, o.id_order,o.id_state, o.id_cart, o.ps_reference, a.firstname, a.lastname, 
    a.address1,a.address2,a.id_country, oc.tracking_number,a.postcode, a.city, a.telephone, a.mobile, c.name, o.module, oc.colli, 
    oc.weight, o.total_shipping, o.total_products_wt,o.details,o.total_paid_tax_incl, a.id_country, o.id_order,o.order_state,o.invoice,o.order_note
     FROM en_orders o 
    INNER JOIN en_carrier c ON c.id_carrier = o.id_carrier
    INNER JOIN en_address a ON a.id_address = o.id_address_delivery
    INNER JOIN en_order_carrier oc ON oc.id_order = o.id_order
    ORDER BY o.date DESC
    ';
    $stmt = $conn->prepare($sql);
    $resultSet = $stmt->executeQuery();
    return $resultSet->fetchAllAssociative();
}

This function give me an important variable (id_cart), that i will use in the function of the repository ProductRepository.php

    public function getListOfProductWithCart($idCart)
{
    $conn = $this->getEntityManager()->getConnection();
    $sql = '
    SELECT DISTINCT p.id_product, p.name, p.reference, cp.quantity
    FROM en_cart_product cp
    INNER JOIN en_product AS p ON p.id_product = cp.id_product
    WHERE p.id_lang = 1 and cp.id_cart = :id_cart
    ORDER BY o.date DESC
    ';
    $stmt = $conn->prepare($sql);
    $resultSet = $stmt->executeQuery(array('id_cart' => $idCart));
    return $resultSet->fetchAllAssociative();
}

In my template twig, i loop the first array given by the function getFilteredOrders(), and inside the loop of this array i need to loop the function getListOfProductWithCart($id_cart) after passing the $id_cart. But, how can i pass the id_cart inside the controller? Or can i do this inside the twig template?

OrdiniController.php

    class OrdiniController extends AbstractController
{
    /**
     * @Route("/ordini", name="ordini")
     */
    public function index(OrdersRepository $ordersRepository): Response
    {
        return $this->render('ordini/ordini.html.twig',[
            'ordini' => $ordersRepository->getFilteredOrders(),
        ]);
    }
}

ordini.html.twig

                    {% for ordine in ordini %}
                        <tr>
                            <td>some data {{ordine.id_order}}</td>
                            <td>some data <br>
                             {{ordine.date}} <br>
                             {{ordine.en_reference}} <br>
                             ID: {{ordine.id_order}} <br>
                             RIF:: {{ordine.ps_reference}} <br></td>
                            <td> {{ordine.name}}</td>
                            <td>some data</td>
                            <td>**** I NEED TO DO HERE getListOfProductWithCart(ordine.id_cart)                                      *                               AND LOOP THE RESULT ***</td>
                            <td>some data</td>
                        </tr>
                    {% endfor %}   
0 Answers
Related