Last active
August 29, 2015 14:02
-
-
Save wogsland/4be962bfc68b5a51f0c9 to your computer and use it in GitHub Desktop.
Laravel: quickly build models from an existing DB. Put the files in app/models directory, modify build_models.php to your DB info and then run it on the command line. After your models are created, build_models.php can be deleted. The build_models.php script is also be fairly simple to modify to create the controllers for your particular applica…
This file contains hidden or 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 | |
use Rhumsaa\Uuid\Uuid; | |
class BaseModel | |
extends Eloquent | |
{ | |
// Make sure errors are available for when validation fails | |
public $errors; | |
// Make sure unsavable is an array | |
public $unsavable = array(); | |
// By default, models use UUID | |
public $uuid = TRUE; | |
/** | |
* Listener assignment | |
*/ | |
public static function boot() | |
{ | |
parent::boot(); | |
static::saving(function( $model ){ | |
# Validation | |
if ( $model->validate() ) | |
{ | |
if ( count( $model->unsavable ) ) | |
{ | |
foreach ( $model->unsavable as $field ) | |
{ | |
unset( $model->$field ); | |
} | |
} | |
return TRUE; | |
} | |
return FALSE; | |
}); | |
} | |
/** | |
* Global validation method. | |
* | |
* @return boolean | |
*/ | |
public function validate() | |
{ | |
$validation = Validator::make($this->attributes, static::$rules); | |
if ( $validation->passes() ) return TRUE; | |
$this->errors = $validation->messages(); | |
return FALSE; | |
} | |
/** | |
* Return the Model’s rules. | |
* | |
* @return array | |
*/ | |
public function getRules() | |
{ | |
return isset( static::$rules ) ? static::$rules : array(); | |
} | |
/** | |
* Return the Model’s table structure. | |
* | |
* @return array | |
*/ | |
public function getTableColumns( $include_internals=false ) | |
{ | |
$schema = \DB::getDoctrineSchemaManager( $this->table ); | |
$columns = $schema->listTableColumns( $this->table ); | |
$internals = array(); | |
if ( ! $include_internals ) | |
{ | |
$internals = array_merge( | |
$this->guarded, | |
array( 'created_at', 'updated_at', 'deleted_at' ) | |
); | |
} | |
$table = array(); | |
foreach ( $columns as $column ) | |
{ | |
$column = array( | |
'name' => $column->getName(), | |
'type' => $column->getType()->getName(), | |
'length' => $column->getLength(), | |
'default' => $column->getDefault() | |
); | |
if ( ! in_array( $column['name'], $internals ) ) | |
{ | |
$table[] = $column['name']; | |
} | |
} | |
return $table; | |
} | |
} |
This file contains hidden or 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 | |
$dev_host = 'host'; | |
$dev_database = 'database'; | |
$dev_user = 'username'; | |
$dev_password = 'password'; | |
$devDB = new mysqli($dev_host, $dev_user, $dev_password); | |
$result = $devDB->query("SELECT distinct t.table_name,(SELECT GROUP_CONCAT(k.COLUMN_NAME SEPARATOR ', ') | |
FROM information_schema.key_column_usage k | |
WHERE t.TABLE_NAME=k.TABLE_NAME | |
AND k.TABLE_SCHEMA = '$dev_database' | |
AND k.CONSTRAINT_NAME = 'PRIMARY') as key_name | |
FROM information_schema.tables t | |
INNER JOIN information_schema.columns c | |
ON t.TABLE_NAME=c.TABLE_NAME AND c.TABLE_SCHEMA = '$dev_database' | |
WHERE t.TABLE_SCHEMA = '$dev_database' | |
"); | |
while ($row = $result->fetch_assoc()) { | |
$table_name = $row['table_name']; | |
$table_id = $row['key_name']; | |
$model_name = rtrim(ucfirst($table_name),'s'); | |
//$table_id = rtrim($table_name,'s')."ID"; | |
$model = "<?php\n\n"; | |
$model .="class $model_name\n"; | |
$model .="extends BaseModel\n"; | |
$model .="{\n\n"; | |
$model .="\t/**\n"; | |
$model .="\t * The database table used by the model.\n"; | |
$model .="\t *\n"; | |
$model .="\t * @var string\n"; | |
$model .="\t */\n"; | |
$model .="\tprotected \$table = '$table_name';\n\n"; | |
$model .="\t/**\n"; | |
$model .="\t * Set primary key\n"; | |
$model .="\t *\n"; | |
$model .="\t * @var array\n"; | |
$model .="\t */\n"; | |
$model .="\tprotected \$primaryKey = '$table_id';\n\n"; | |
$model .="\t/**\n"; | |
$model .="\t * The attributes excluded from the model's JSON form.\n"; | |
$model .="\t *\n"; | |
$model .="\t * @var array\n"; | |
$model .="\t */\n"; | |
$model .="\tprotected \$hidden = array('createdOn');\n\n"; | |
$model .="\t/**\n"; | |
$model .="\t * The attributes guarded from updating\n"; | |
$model .="\t *\n"; | |
$model .="\t * @var array\n"; | |
$model .="\t */\n"; | |
$model .="\tprotected \$guarded = array( '$table_id' );\n\n"; | |
$model .="\t/**\n"; | |
$model .="\t * Validation rules\n"; | |
$model .="\t *\n"; | |
$model .="\t * @var array\n"; | |
$model .="\t */\n"; | |
$model .="\tprotected static \$rules = array();\n\n"; | |
$model .="}"; | |
$f = fopen($model_name.".php",'w'); | |
fwrite($f,$model); | |
fclose($f); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment