Welcome Guest, Not a member yet? Register   Sign In
Namespaces support for seeds
#1

(This post was last modified: 03-20-2023, 08:13 AM by Muzikant.)

Hi. There is a namespaces support for migrations

Code:
php spark migrate -n 'Acme\Blog'

but there is no namespaces support for seeds

Code:
php spark db:seed TestSeeder

I am separating common things for all of my projects into special namespace. There are not only tables, but also seeds. For example, I want to have special table with all towns zip codes, which should be common for every e-commerce website for autocomplete. Migrations and seeds could be great for this.

Why is seeding part of DB and not separated as migrations?
Reply
Reply
#3

(This post was last modified: 03-21-2023, 01:19 AM by Muzikant.)

(03-20-2023, 10:12 AM)superior Wrote: See: https://forum.codeigniter.com/showthread...#pid395667

Thank you for your answer. It does not work for me.

I have a fresh installation of CodeIgniter 4.3.2 and I am using PHP 8.1.12 on my Linux machine.

app/Config/Autoload.php
PHP Code:
// ...
public $psr4 = [
    APP_NAMESPACE => APPPATH// For custom app namespace
    'Config'      => APPPATH 'Config',
    'Novelist'    => ROOTPATH 'novelist',
];
// ... 

novelist/Database/Migrations/2023-03-20-193510_Alphabet.php
PHP Code:
<?php
namespace Novelist\Database\Migrations;
use 
CodeIgniter\Database\Migration;
class 
Alphabet extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => ['type' => 'INT''unsigned' => true'auto_increment' => true],
            'letter' => ['type' => 'VARCHAR''constraint' => '1'],
        ]);
        $this->forge->addKey('id'true);
        $this->forge->createTable('alphabet');
    }
    public function down()
    {
        $this->forge->dropTable('alphabet');
    }


novelist/Database/Seeds/Alphabet.php
PHP Code:
<?php
namespace Novelist\Database\Seeds;
use 
CodeIgniter\Database\Seeder;
class 
Alphabet extends Seeder
{
    public function run()
    {
        $alphabet = ['a''b''c''d''e''f''g''h''i''j''k''l''m''n''o''p''q''r''s''t''u''v''w''x''y''z'];
        $data = [];
        foreach ($alphabet as $letter) {
            $data[] = [
                'letter' => $letter,
            ];
        }
        $this->db->table('alphabet')->insertBatch($data);
    }


Command:
Code:
php spark db:seed Novelist\Database\Seeds\Alphabet

CodeIgniter v4.3.2 Command Line Tool - Server Time: 2023-03-20 18:59:00 UTC+00:00
[InvalidArgumentException]
The specified seeder is not a valid file: app/Database/Seeds/NovelistDatabaseSeedsAlphabet.php
at SYSTEMPATH/Database/Seeder.php:131
Backtrace:
  1    SYSTEMPATH/Commands/Database/Seed.php:77
       CodeIgniter\Database\Seeder()->call('NovelistDatabaseSeedsAlphabet')
  2    SYSTEMPATH/CLI/Commands.php:65
       CodeIgniter\Commands\Database\Seed()->run([])
  3    SYSTEMPATH/CLI/Console.php:37
       CodeIgniter\CLI\Commands()->run('db:seed', [...])                                            
  4    ROOTPATH/spark:97
       CodeIgniter\CLI\Console()->run()

Am I doing something wrong?
Reply
#4

(This post was last modified: 03-20-2023, 05:29 PM by kenjis.)

Code:
php spark db:seed Novelist\\Database\\Seeds\\Alphabet

See https://codeigniter4.github.io/CodeIgnit...ml#migrate
Reply
#5

(This post was last modified: 03-21-2023, 01:11 AM by Muzikant.)

(03-20-2023, 04:11 PM)kenjis Wrote:
Code:
php spark db:seed Novelist\\Database\\Seeds\\Alphabet

See https://codeigniter4.github.io/CodeIgnit...ml#migrate

Oh, sure... It works, thank you.

These informations should be added to the seeding documentation page:
https://www.codeigniter.com/user_guide/d...seeds.html
Reply




Theme © iAndrew 2016 - Forum software by © MyBB