Created
December 7, 2011 07:02
-
-
Save eirworks/1441799 to your computer and use it in GitHub Desktop.
A FuelPHP task example for seeding like in the Rails.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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