Welcome Guest, Not a member yet? Register   Sign In
Trying to make recursive function : no data return
#1

(This post was last modified: 12-19-2022, 01:20 AM by sTis. Edit Reason: Change code )

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 ?
Reply
#2

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?
Reply
#3
Wink 

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
Reply
#4

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');
Reply
#5

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
Reply
#6

This explains on how to write recursive php methods.

PHP Recursive Functions: How to Write Them, and Why They’re Useful
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

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 ?
Reply
#8

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

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.
Reply
#10

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

Forums - Convert array structure for ul() codeigniter helper
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB