Symfony 3:以其他方法转发方法的结果

时间:2018-08-01 13:11:49

标签: symfony-3.4

我正在尝试将此方法的结果插入同一控制器中的另一个方法:

public function categorieAction(Request $request, string $categorie) {

        $categories = $this->getDoctrine()->getRepository(Categorie::class)->findAll();

        $criteria = 'p.subcat_id=sub.id and sub.categorie_id=cat.id and cat.nom like "' . str_replace('-', ' ', $categorie) . '"';
        $em = $this->getDoctrine()->getManager();

        $query1 = 'SELECT p.*,(SELECT min(h.prix) FROM Histprix h,Urlproduit u
                    WHERE h.urlproduit_id=u.id and u.produit_id=p.id  and p.status=1) as "prixmin",(SELECT max(h.prix) FROM Histprix h,Urlproduit u
                    WHERE h.urlproduit_id=u.id and u.produit_id=p.id  and p.status=1) as "prixmax", (select count(*) from produit_revendeur  where produit_id=p.id and p.status=1) as "offre"
                FROM  Produit p,Subcat sub,Categorie cat
                WHERE p.status=1 and ' . $criteria . ' ;';

        $query2 = 'SELECT DISTINCT s.* FROM Spec s, Produit p, Valeur v, Produit_valeur vp
                WHERE vp.product_id=p.id AND vp.valeur_id = v.id AND s.id=v.spec_id AND p.id IN 
                (SELECT p.id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and ' . $criteria . ')';

        $query3 = 'SELECT DISTINCT valeur, spec_id from Valeur v, produit_valeur pv where pv.product_id IN  (SELECT p.id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and p.subcat_id=sub.id and sub.categorie_id=cat.id and cat.nom like "' . $categorie . '") and v.id=pv.valeur_id';

        $query4 = 'SELECT DISTINCT m.nom from Marque m where m.id IN (SELECT p.marque_id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and ' . $criteria . ')';

        $query5 = 'SELECT distinct min(h.prix) as "min" ,max(h.prix) as "max" FROM Histprix h,Urlproduit u,Produit p
                    WHERE h.urlproduit_id=u.id and u.produit_id=p.id and p.status=1 AND p.id IN 
                (SELECT p.id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and ' . $criteria . ')';
        $statement = $em->getConnection()->prepare($query1);
        $statement->execute();
        $produit = $statement->fetchAll();

        $statement1 = $em->getConnection()->prepare($query2);
        $statement1->execute();
        $specs = $statement1->fetchAll();

        $statement2 = $em->getConnection()->prepare($query3);
        $statement2->execute();
        $valeurs = $statement2->fetchAll();

        $statement3 = $em->getConnection()->prepare($query4);
        $statement3->execute();
        $brands = $statement3->fetchAll();

        $statement4 = $em->getConnection()->prepare($query5);
        $statement4->execute();
        $prices = $statement4->fetchAll();

        if (!$produit) {
            throw $this->createNotFoundException('The product does not exist');
        } else {
            $paginator = $this->get('knp_paginator');
            $produits = $pagination = $paginator->paginate(
                    $produit, $request->query->getInt('page', 1), $request->query->getInt('limit', 5));

            return $this->render('ProductBundle:Product:category.html.twig', array('produits' => $produits, 'specs' => $specs, 'valeurs' => $valeurs, 'brands' => $brands, 'price' => $prices,'categories'=>$categories));
        }
    }

这是第二种方法,其中我使用 forward 来访问生成结果的方法,而不是将其用作服务(最佳实践)。问题是如何在 contactusAction()中发送 categorieAction() category 参数:

public function contactusAction() {
      //contactusAction()" requires that you provide a value for the "$c" argument
        $categories = $this->forward('ProductBundle:Product:categorie', array(
        'c'  => $c,
    ));
        return $this->render('ProductBundle:Default:contactus.html.twig', array('categories'=>$categories));
    }

1 个答案:

答案 0 :(得分:0)

解决方案是使用forward:

import numpy as np
import matplotlib.pyplot as plt

rand_data = np.random.uniform(0, 1.0, 100)

fig = plt.figure()

ax_1 = fig.add_subplot(211)
ax_1.hist(rand_data, bins=10)

ax_2 = fig.add_subplot(212)
ax_2.hist(rand_data, bins=100)

plt.show()