Welcome Guest, Not a member yet? Register   Sign In
Run seed command in module
#1

I have create an Admin module, to add an Admin user into the db I created a seed file into the Admin\Database\Seeds path, below the code of the seeder
Code:
namespace Admin\Database\Seeds;

use CodeIgniter\Database\Seeder;
use CodeIgniter\Shield\Entities\User;
use App\Models\UserModel;

class AddAdminAccount extends Seeder
{
    public function run()
    {
        $user = new User([
            'email' => '[email protected]',
            'password' => 'secret',
            'firstname' => 'Admin',
            'lastname' => 'Strator',
        ]);

        $model = new UserModel;

        $model->save($user);

        $user = $model->findById($model->getInsertID());

        $user->activate();

        $user->addGroup('user', 'admin');
    }
}

The issue arises when I run the php spark dbConfusedeed NameFile command...I got the following error, since it seems that the command looks for the seeder file into the main app folder.
[InvalidArgumentException]
The specified seeder is not a valid file: D:\project\app\Database/Seeds/AddAdminAccount
I'm sure that could be an option to set spark db command to run in Admin module namespace...but I wasn't able to find it in CI documentation...am I wrong? 
Is it possible to run seed file in Admin module?
Thanks a lot for your feedback
Reply
#2

You can get all spark commands from your terminal using this command:
Code:
php spark list

For namespaces it shows here how to do it:
CodeIgniter 4 Users Guide - Managing Databases - Database Seeding
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 01-06-2025, 01:16 AM by Vespa.)

Thanks for feedback InsiteFX.
I knew about spark list command...in this case the level of info details in that command is useless to fix my issue, even the php spark db seed --help command doesn't provide any useful info.

I knew the Managing Database Seeding page in CI web site...I followed that info ( and the Dave Hollingworth course ) to create the seed file into the module Admin folder...using the command
Code:
php spark make:seeder AddAdminAccount --namespace Admin

Unfortunately I don't find any command to run the seed file in the Admin namespace
The following command:
Code:
php spark db:seed AddAdminAccount

return the following error
Code:
The specified seeder is not a valid file: D:\Aero Club Belluno\composer-aeclidb\app\Database/Seeds/AddAdminAccount.php


I tried to add namespace, with
Code:
php spark db:seed AddAdminAccount --namespace Admin


But again the same error

I'm doing something wrong...I'm sure the fault is on my side...but from the documentation around here I cannot find the fix
Again, thanks for taking the time to help
Reply
#4

(This post was last modified: 01-06-2025, 03:27 AM by InsiteFX. Edit Reason: add other stuff to reply )

This does not look right, look at the the slashes some are back slashes and others are forward slashes
they should all be the same not mixed.

Code:
D:\Aero Club Belluno\composer-aeclidb\app\Database/Seeds/AddAdminAccount.php


I'll see if I can find some time later to look deeper in to this problem.

Also I would stay away from using spaces in file and folder names they can cause problems.
What did you Try? What did you Get? What did you Expect?

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

Thanks a lot for all your support InsiteFX.
Me too, I noticed the weird mixed slashes in the error message ...and you are right...better stay away from using spaces in file and folder names they can cause problem...I can live for sure with seeder into their standard app\Database\Seedes folder...I was just curious to understand the reason the seed didn't work even if we can create it into module as official Documentation says....maybe we found a bug, who knows.
In any case, it would be fine if the seeder could be run in Module...it could come in handy to develop a self-contained module structure...just my 2 cents, of course Smile
Again, thanks a lot for taking the time to look into it and for all your priceless support.
Reply
#6

One thing that is critical is that you need to have the module path in the Autoload.php file like below
for it to find the seeder in a module.

PHP Code:
public $psr4 = [
    APP_NAMESPACE => APPPATH,
    // InsiteFX Code Modules.
    'InsiteFX\Admin'        => ROOTPATH 'InsiteFX/Admin',          // InsiteFX/Admin Module
    'InsiteFX\Blog'        => ROOTPATH 'InsiteFX/Blog',          // InsiteFX/Blog Module
    // Shield Auth Modules.
    'CodeIgniter\Settings'  => ROOTPATH 'InsiteFX/settings/src',  // Settings Module
    'CodeIgniter\Shield'    => ROOTPATH 'InsiteFX/shield/src',    // Shield Auth Module
]; 
What did you Try? What did you Get? What did you Expect?

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

I Created the Migration and Seeder files using the namespace and both files where created
in InsiteFX/Blog.

Code:
php spark make:migration BlogTables --namespace InsiteFX/Blog

php spark make:seeder BlogSeeder --namespace InsiteFX\Blog

I'll test this out later today and let you know if I come across any problems.
What did you Try? What did you Get? What did you Expect?

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

Ok, I just ran it and I get the same error as you do, I have created a Bug Report on GitHub.

We just have to wait now for an answer.
What did you Try? What did you Get? What did you Expect?

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

Ok, there was an error in my Seeder file that was causing my problems but all works now

For Seeding from the App folder:
Code:
php spark db:seed YourSeederName

For Seeding from the Modules folder - must pass the namespace:
Code:
php spark db:seed Modules\YourModuleName\Database\Seeds\YourSeederName

If you still have problems please check your files for errors etc.
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