Skip to content

Instantly share code, notes, and snippets.

@epok07
Forked from eirworks/seed.php
Created February 9, 2018 05:28
Show Gist options
  • Save epok07/8533c2ad1bf42c695ef8d0e389377cb9 to your computer and use it in GitHub Desktop.
Save epok07/8533c2ad1bf42c695ef8d0e389377cb9 to your computer and use it in GitHub Desktop.
A FuelPHP task example for seeding like in the Rails.
<?php
#seeding data
namespace Fuel\Tasks;
class Seed
{
public static function run()
{
$projects = array("Example Project Title"=>"Example Project description that descripting nothing...",
"Another title " => "Description that does nothing at all...",
);
foreach ($projects as $title => $description) {
$p = \Model\Project::forge();
$p->name = $title;
$p->description = $description;
$p->save();
echo "Saving $title\n";
echo "Seeding done!";
}
}
# Reset all records in certain model, eg:
# php oil r seed:reset 'project';
# note the model name using a singularized name!
public static function reset($modelname='')
{
if ($modelname)
{
# Since models are singularized name,
# the model name must be pluralized
\DBUtil::truncate_table(\Inflector::pluralize($modelname));
echo "All records on model $modelname successfully reset";
}
else {
// I dont know how to list all models so I can directly
// reset all tables...
// So you must specify the model name instead like
// example above... :(
echo "Please specify a model name!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment