ClosureTable icon indicating copy to clipboard operation
ClosureTable copied to clipboard

createFromArray

Open mefenlon opened this issue 5 years ago • 2 comments

Using "franzose/closure-table": "^6.1", "laravel/framework": "^6.2",

When using demo code from https://github.com/franzose/ClosureTable#tree

class SubjectsTableSeeder extends Seeder

{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Subject::createFromArray([
            'id' => 1,
            'children' => [
                [
                    'id' => 2,
                    'children' => [
                        [
                            'id' => 3,
                            'children' => [
                                [
                                    'id' => 4,
                                    'children' => [
                                        [
                                            'id' => 5,
                                            'children' => [
                                                [
                                                    'id' => 6,
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]);
    }

Running the seeder results in this error In Entity.php line 116: Argument 1 passed to Franzose\ClosureTable\Models\Entity::__construct() must be of the type array, int given, called in /home/vagrant/code/ripm_lill_laravel/vendor/franzose/closure-table/src/Models/Entity.php on line 1770

After adding another level of nesting as suggested in #239

       Subject::createFromArray([
            [
                'id' => 1,
                'children' => [
                    [
                        'id' => 2,
                        'children' => [
                            [
                                'id' => 3,
                                'children' => [
                                    [
                                        'id' => 4,
                                        'children' => [
                                            [
                                                'id' => 5,
                                                'children' => [
                                                    [
                                                        'id' => 6,
                                                    ]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]);

I get the error

In Str.php line 419: Array to string conversion

I believe this issue is related to the nested children. Removing any 'children' results in a successful migration.

        Subject::createFromArray([

                ['id' => 200],
                ['id' => 300],
                ['id' => 400],
                ['id' => 500],

        ]);

mefenlon avatar Feb 01 '21 17:02 mefenlon

@mefenlon I have to say sorry for not working on the package for so long. I understand that it feels like the package is abandoned. But I just need to find a passion again to work on it since I haven't worked with Laravel itself for quite a while.

Let's keep the issue open so that it is clear the thing still works incorrectly and needs to be fixed.

franzose avatar Jan 18 '25 07:01 franzose

if I am not wrong,

createFromArray() passes $item (which includes "children" key ) into the model’s constructor (new static($item)). When it tries to insert into the database, it causes error because children column is not exist. unset is needed.

unset($item[static::CHILDREN_RELATION_NAME]); after line 1765 at Entity.php

naimhasim avatar Feb 09 '25 04:02 naimhasim