0

Here in indexAction I must call to subcat function to recursively get a list of categories and subcategories, otherwise it does not work.

Note subcat calls itself.

How can I write subcat function outside indexAction? and call it from inside IndexAction. Doing this, i can reuse subcat.

class ItemsController implements ControllerProviderInterface {
    public function connect(Application $app) {
        $controllers = $app['controllers_factory'];

        $controllers
        ->get('/', array($this, 'indexAction'))
        ->bind('items_index');

        return $controllers;
    }

    public function indexAction(Application $app) {
        $categorias = array();

        function subcat($idPadre, array & $categorias, $app) {
            $sql = 'SELECT id, categoria FROM categorias' .
                ' WHERE id_padre=' . $idPadre .
                ' ORDER BY categoria';
            $query = $app['db']->fetchAll($sql);

            foreach ($query as $valor) {
                $a = subcat($valor['id'], $categorias, $app);
                $lista[$valor['id'].'-'.$valor['categoria']] = $a;
            }

            return $lista;
        };

        $estructura = subcat(0, $categorias, $app);
        print_r($estructura);
        return '';
    }    
}

3 Answers 3

0

You could register your reusable code as a service. You should find anything you need here: LINK

0
0

I solved this doing the service, where $app['Categorias.lista'] contain all the categories:

$app['Categorias.idPadre'] = 0;
$app['Categorias.lista'] = '';
$app['Categorias'] = $app->share(function ($app) {
    function subcat($idPadre, $app) {
        $sql = 'SELECT id, categoria FROM categorias' .
                ' WHERE id_padre=' . $idPadre .
                ' ORDER BY categoria';
        $query = $app['db']->fetchAll($sql);

        foreach ($query as $valor) {
            $lista[$valor['id'].'-'.$valor['categoria']] = subcat($valor['id'], $app);
        }

        return $lista;
    };

    $app['Categorias.lista'] = subcat($app['Categorias.idPadre'], $app);
});

then I test it in my indexAction method:

$app['Categorias.idPadre'] = 0;
$app['Categorias'];
print_r($app['Categorias.lista']);

and print_r gives this in my example:

Array
(
    [2-capacitor] => Array
        (
            [14-ceramico] => 
            [12-electrolitico] => 
            [13-multicapa] => 
            [16-polipropileno] => 
            [15-polyester] => 
        )

    [3-circuito impreso] => 
    [4-diodo] => 
    [1-resistencia] => Array
        (
            [11-ceramica] => 
            [5-metal] => Array
                (
                    [8-1W] => 
                    [9-2W] => 
                    [10-3W] => 
                    [7-mini] => 
                )

        )

    [20-teclado] => 
    [21-transformador] => 
    [17-transistor] => Array
        (
            [18-bipolar] => 
            [19-mosfet] => 
        )

)
0

All you have to do is make your inner function a private function in your ItemsController.

class ItemsController implements ControllerProviderInterface {
    public function connect(Application $app) {
        $controllers = $app['controllers_factory'];

        $controllers
        ->get('/', array($this, 'indexAction'))
        ->bind('items_index');

        return $controllers;
    }

    public function indexAction(Application $app) {
        $categorias = array();

        $estructura = $this->subcat(0, $categorias, $app);
        print_r($estructura);
        return '';
    }

    private function subcat($idPadre, array & $categorias, $app) {
        $sql = 'SELECT id, categoria FROM categorias' .
            ' WHERE id_padre=' . $idPadre .
            ' ORDER BY categoria';
        $query = $app['db']->fetchAll($sql);

        foreach ($query as $valor) {
            $a = $this->subcat($valor['id'], $categorias, $app);
            $lista[$valor['id'].'-'.$valor['categoria']] = $a;
        }

        return $lista;
    }
}
3
  • thanks! i am sure i try this and does not work, not sure why, is very simple, but will try again and post results
    – Guillermo
    Commented Jun 25, 2015 at 0:18
  • it does not work, if i write simple code in subcat it works, but if i need to use $app it crash
    – Guillermo
    Commented Jun 27, 2015 at 1:05
  • I am quite sure this works. I've used this pattern many times, but I wrote up a proof of concept with this code just to make sure. I'm sure the problem is somewhere else. Maybe in the db service? It's impossible to say without seeing more of your code. Commented Jun 27, 2015 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.