1

I am using Laravel 4 with MySQL back-end.

I want to store records of a nested array into database using recursive function.

The input array is as below :

Array
(
    [0] => Array
    (
        'id' => 561,
        'type' => 'q',
        'subtype' => 'boolean',
        'title' => 'Did you..?',
        'parent' => 560,
        'created_at' => '2014-07-09 09:45:50',
        'updated_at' => NULL,
        'deleted_at' => NULL,
        'children' => Array
            (
                [0] => Array
                    (
                        'id' => 562,
                        'type' => 'o',
                        'subtype' => NULL,
                        'title' => 'Yes',
                        'parent' => 561,
                        'created_at' => '2014-07-09 09:45:50',
                        'updated_at' => NULL,
                        'deleted_at' => NULL,
                        'children' => Array
                            (
                            )
                    )
                [1] => Array
                    (
                        'id' => 563,
                        'type' => 'answer',
                        'subtype' => NULL,
                        'title' => 'No',
                        'parent' => 561,
                        'created_at' => '2014-07-09 09:45:50',
                        'updated_at' => 'NULL',
                        'deleted_at' => NULL,
                        'children' => Array
                            (
                            )
                    )
            )
    )
)

The recursive function I am using store the records into the database is as below :

public function addRecursiveChildren(array $surveychildren, $parentid, $userid){

    foreach ($surveychildren as $item) 
    {
        /* Error : HTTPRequest Error :: 500: {"error":{"type":"ErrorException","message":"Cannot use a scalar value as an array
           Error is in the statement below in the second recursion when child is passes as an input.
        */
        $item['survey_id'] = $item['id']; 
        $item['user_id'] = $userid;
        $item['id'] = null; 
        $item['parent'] = $parentid; 

        $routine = routine::create($item);

        if(count($item["children"]) > 0)
        {
            foreach ($item["children"] as $child) 
            {
                /* The $child I found is as below : 
                $child = array(
                    'id' => 562,
                    'type' => 'o',
                    'subtype' => NULL ,
                    'title' => 'Yes',
                    'parent' => 561,
                    'created_at' => '2014-07-09 09:45:50',
                    'updated_at' => NULL,
                    'deleted_at' => NULL,
                    'children' => Array
                        (
                        )
                );
                */

                RoutineRepository::addRecursiveChildren($child, $routine->id, $userid);
            }
        }
    }
}

Edit :

I know that cause of error is the $child I am passing as an input array to the recursive function above :

The $child is something like this :

array(
    'id' => 562,
    'type' => 'o',
    'subtype' => NULL ,
    'title' => 'Yes',
    'parent' => 561,
    'created_at' => '2014-07-09 09:45:50',
    'updated_at' => NULL,
    'deleted_at' => NULL,
    'children' => Array
    (
    )
)

Instead of this if $child will be something like this :

Array
(
    [0] =>
    array(
        'id' => 562,
        'type' => 'o',
        'subtype' => NULL ,
        'title' => 'Yes',
        'parent' => 561,
        'created_at' => '2014-07-09 09:45:50',
        'updated_at' => NULL,
        'deleted_at' => NULL,
        'children' => Array
        (
        )
    )
)

Then there will be no error.

Can anybody help me to overcome it?

Thanks.

6
  • Maybe all you need is a simple insert multiple times laravel.com/docs/queries#inserts ? Commented Jul 28, 2014 at 6:13
  • I'm not sure why you need a recursive function, Laravel can handle multiple insertions at one time Commented Jul 28, 2014 at 6:15
  • I cannot add it directly because I need to insert it with parent child relationship in a self-referencing table. I am not sure if it will handle this relationship automatically. Commented Jul 28, 2014 at 6:17
  • Is the parent ID for the batch of items the same whenever you call this function, or can there be random parent IDs and children each time? Commented Jul 28, 2014 at 6:19
  • Nope. Look into the input array. By inserting the first element into the database, it will return you the primary key of that record and that primary key will be used as a parent of nested array (children) while storing record into the database. Commented Jul 28, 2014 at 6:22

1 Answer 1

2

This should work

class Routine extends \Eloquent
{
    // The relation
    public function subRoutine()
    {
        return $this->hasMany('Routine', 'parent');
    }

    public function saveSubroutine(array $children)
    {
        foreach($children as $childData)
        {
            $child = new self($childData);
            $this->subRoutine()->save($child);

            $child->saveSubroutine($childData['children']);
        }
    }
}

$routine = Routine::create($data);
$routine->saveSubroutine($data['children']);
Sign up to request clarification or add additional context in comments.

1 Comment

Although I have resolved it differently as mentioned in my comment of question, I am accepting it as better solution. and +1 for the efforts you did for me. Thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.