CodeIgniter Forums
Trying to make recursive function : no data return - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Trying to make recursive function : no data return (/showthread.php?tid=85126)



Trying to make recursive function : no data return - sTis - 12-04-2022

Hello,

I'm trying to make a function that get data recursively to get parents of an entity. And also parents of the parents.

If an entity is at the root is parent is set to null.

My database :

Code:
--
-- Structure de la table `test`
--

DROP TABLE IF EXISTS `test`;
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `label` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Déchargement des données de la table `test`
--

INSERT INTO `test` (`id`, `parent_id`, `label`) VALUES
(1, NULL, 'entity 1'),
(2, NULL, 'entity2'),
(3, 1, 'entity 3'),
(4, 1, 'entity 4'),
(5, 3, 'Entity 5');
COMMIT;


My controller (Edited 19/12) :
PHP Code:
function getAllEntities() {
        $testsMdl model('testsModel');

        $id "5";

        $test $testsMdl->getParentsRecursive($id);

        echo"<pre>";
        echo var_dump($test);
        echo "</pre>";
    



My model (Edited 19/12) :

PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
TestsModel extends Model {

    function getParentsRecursive(int $id, array $data null) {
        $child $this->getEntity($id);

        $data[] = $child;

        if ($child->parent_id != null) {
            $this->getParentsRecursive($child->parent_id$data);
        } else {
            echo"<pre>";
            echo var_dump($data);
            echo "</pre>";
            return $data;
        }
    }

    function getEntity(string $id) {
        $builder $this->db->table('test');
        $builder->select('id, parent_id, label')
                ->where('id'$id);
        $qr $builder->get();
        return $qr->getRow();
    




$data is populated with the right data as you can see (var_dump in model) but, return $data doesn't work !!

Code:
C:\wamp64\www\qqov2\app\Models\TestsModel.php:18:
array (size=3)
  5 =>
    object(stdClass)[82]
      public 'id' => string '5' (length=1)
      public 'parent_id' => string '3' (length=1)
      public 'label' => string 'Entity 5' (length=8)
  3 =>
    object(stdClass)[84]
      public 'id' => string '3' (length=1)
      public 'parent_id' => string '1' (length=1)
      public 'label' => string 'entity 3' (length=8)
  1 =>
    object(stdClass)[86]
      public 'id' => string '1' (length=1)
      public 'parent_id' => null
      public 'label' => string 'entity 1' (length=8)
C:\wamp64\www\qqov2\app\Controllers\Tests.php:41:null


Not any data return to my controller. Var_dump in my controller return NULL :

Code:
C:\wamp64\www\qqov2\app\Controllers\Tests.php:41:null




I'm probably missing something but can see what it is !!!!

Can someone help me please ?


RE: Trying to make recursive function : no data return - bassmandc - 12-10-2022

If you're able to verify that $data is populated with the right data before it hits the return function, then I don't think it's the return function that isn't working.  I think the problem lies with this line:

$test = $testsMdl->getParentsRecursive($id, array());

I might be missing something, but I don't understand what "array()" is doing in that call.  Try this:

$test = $testsMdl->getParentsRecursive($id);

Does it return your data?


RE: Trying to make recursive function : no data return - sTis - 12-19-2022

Hi,
Thank for your answer. Sorry for the long time before my return.
I've edited my code (see in my first post).
I've try to eliminate the array as you suggested. But, the array() is useful for compiling data before sending it.
I removed the array parameter in the call to my model's function in the controller.
As soon as I call the function getParentsRecursive itself the return doesn't work anymore.

Every suggestion welcome, Huh


RE: Trying to make recursive function : no data return - bassmandc - 12-19-2022

Go ahead and restore the "array()" definition in your call then.  Here's my next suggestion:

In your Controller, change the line:


Code:
$testsMdl = model('testsModel');

to either: 


Code:
$testsMdl = model(testsModel::class);

or


Code:
$testsMdl = new model('testsModel');



RE: Trying to make recursive function : no data return - badger - 12-19-2022

Don't know how many records you have in your database but in a similar situation, rather than hitting the db with umpteen queries, i extract all the entries and then do a recursive loop in php
Bill


RE: Trying to make recursive function : no data return - InsiteFX - 12-20-2022

This explains on how to write recursive php methods.

PHP Recursive Functions: How to Write Them, and Why They’re Useful


RE: Trying to make recursive function : no data return - sTis - 12-20-2022

Thanks all for your answers,
@bassmanc : I've tried yours propositions. It's not the solution. In fact my model call works : I've got results in my recursive function in app/models/testsModel.php.
@badger : I used such a solution to make it work. But I really want to understand why my results can't go out of my model !!!! 
@InsideFX : thanks for this article.
Any idea on what my result can't be return ?


RE: Trying to make recursive function : no data return - InsiteFX - 12-21-2022

If return $data; not working then there is something wrong with your if statement.

Try retruning the data ant the end and see if it returns without the if statement.


RE: Trying to make recursive function : no data return - sTis - 12-25-2022

What's stange is that $data in else statement are right (see below). In my example, I have 3 entities to return. $data is correctly populated.
everything seems to work fine except the return function.
PHP Code:
$data in Else loop
C
:\wamp64\www\qqov2\app\Models\TestsModel.php:18:
array (
size=3)
  0 => 
    object(stdClass)[82]
      public 'id' => string '5' (length=1)
      public 'parent_id' => string '3' (length=1)
      public 'label' => string 'Entity 5' (length=8)
  1 => 
    object(stdClass)[84]
      public 'id' => string '3' (length=1)
      public 'parent_id' => string '1' (length=1)
      public 'label' => string 'entity 3' (length=8)
  2 => 
    object(stdClass)[86]
      public 'id' => string '1' (length=1)
      public 'parent_id' => null
      
public 'label' => string 'entity 1' (length=8
I've tried your proposal. I did a var_dump out of the if statement.
PHP Code:
$data dump outside of if loop
C
:\wamp64\www\qqov2\app\Models\TestsModel.php:23:
array (
size=3)
  0 => 
    object(stdClass)[82]
      public 'id' => string '5' (length=1)
      public 'parent_id' => string '3' (length=1)
      public 'label' => string 'Entity 5' (length=8)
  1 => 
    object(stdClass)[84]
      public 'id' => string '3' (length=1)
      public 'parent_id' => string '1' (length=1)
      public 'label' => string 'entity 3' (length=8)
  2 => 
    object(stdClass)[86]
      public 'id' => string '1' (length=1)
      public 'parent_id' => null
      
public 'label' => string 'entity 1' (length=8)

$data dump outside of if loop
C
:\wamp64\www\qqov2\app\Models\TestsModel.php:23:
array (
size=2)
  0 => 
    object(stdClass)[82]
      public 'id' => string '5' (length=1)
      public 'parent_id' => string '3' (length=1)
      public 'label' => string 'Entity 5' (length=8)
  1 => 
    object(stdClass)[84]
      public 'id' => string '3' (length=1)
      public 'parent_id' => string '1' (length=1)
      public 'label' => string 'entity 3' (length=8)
 
$data dump outside of if loop
C
:\wamp64\www\qqov2\app\Models\TestsModel.php:23:
array (
size=1)
  0 => 
    object(stdClass)[82]
      public 'id' => string '5' (length=1)
      public 'parent_id' => string '3' (length=1)
      public 'label' => string 'Entity 5' (length=8

In my example, I have three passages in the recursive function. And you can see above that $data is correctly fed at each pass.
But getParentsRecursive() still return nothing.


RE: Trying to make recursive function : no data return - InsiteFX - 12-26-2022

Please see this post that I answered here for a Category Menu.

Forums - Convert array structure for ul() codeigniter helper