具有嵌套数组的PHP Laravel递归函数

时间:2014-07-28 06:04:31

标签: php laravel laravel-4

我正在使用Laravel 4后端MySQL

我想使用nested arrayrecursive function的记录存储到数据库中。

输入数组如下:

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
                            (
                            )
                    )
            )
    )
)

我使用的recursive function将记录存储到数据库中如下:

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);
            }
        }
    }
}

修改:

我知道错误原因是$child我作为input array传递给上面的recursive function

$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
    (
    )
)

如果$child将是这样的话,而不是这样:

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 个答案:

答案 0 :(得分:2)

这应该有效

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']);