Skip to content

Commit 979e1cf

Browse files
committed
Allow seed classes to exist anywhere.
1 parent b6a9c3b commit 979e1cf

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

system/Database/Seeder.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,31 @@ public function call(string $class)
129129
{
130130
throw new \InvalidArgumentException('No Seeder was specified.');
131131
}
132-
133-
$path = $this->seedPath.str_replace('.php', '', $class).'.php';
134-
135-
if (! is_file($path))
136-
{
137-
throw new \InvalidArgumentException('The specified Seeder is not a valid file: '. $path);
138-
}
139-
140-
if (! class_exists($class, false))
141-
{
142-
require $path;
143-
}
144132

145-
$seeder = new $class($this->config);
133+
$path = str_replace('.php', '', $class).'.php';
134+
135+
// If we have namespaced class, simply try to load it.
136+
if (strpos($class, '\\') !== false)
137+
{
138+
$seeder = new $class($this->config);
139+
}
140+
// Otherwise, try to load the class manually.
141+
else
142+
{
143+
$path = $this->seedPath.$path;
144+
145+
if (! is_file($path))
146+
{
147+
throw new \InvalidArgumentException('The specified Seeder is not a valid file: '. $path);
148+
}
149+
150+
if (! class_exists($class, false))
151+
{
152+
require $path;
153+
}
154+
155+
$seeder = new $class($this->config);
156+
}
146157

147158
$seeder->run();
148159

user_guide_src/source/database/seeds.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ but organize the tasks into separate seeder files::
4949
}
5050
}
5151

52+
You can also use a fully-qualified class name in the **call()** method, allowing you to keep your seeders
53+
anywhere the autoloader can find them. This is great for more modular code bases::
54+
55+
public function run()
56+
{
57+
$this->call('UserSeeder');
58+
$this->call('My\Database\Seeds\CountrySeeder');
59+
}
60+
5261
Using Seeders
5362
=============
5463

0 commit comments

Comments
 (0)