Skip to content

Commit

Permalink
fixed issue with hard coded table name
Browse files Browse the repository at this point in the history
added TABLE_PHP example in model.tpl
  • Loading branch information
Perturbatio committed Jun 6, 2012
1 parent fa2e04b commit f8823fa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
27 changes: 14 additions & 13 deletions classes/generators/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,34 @@ private function _controller_generation()

if ( $this->_table !== '' ){
$pdo = DB::connection(Config::get('database.default'))->pdo;

$all_columns = $pdo->query('DESCRIBE ' . $this->_table )->fetchAll( PDO::FETCH_COLUMN );
$table_primary = $pdo->query("SHOW INDEX FROM tasks WHERE Key_name = 'primary'")->fetchAll( PDO::FETCH_ASSOC );

if ( count( $table_primary ) > 0){
$primary_keys = $pdo->query("SHOW INDEX FROM {$this->_table} WHERE Key_name = 'primary'")->fetchAll( PDO::FETCH_ASSOC );

if ( count( $primary_keys ) > 0){

if (count( $table_primary ) > 1){
$table_primary_temp = array();
foreach($table_primary as $primary){
$table_primary_temp[] = $primary['column_name'];
if (count( $primary_keys ) > 1){
$primary_keys_temp = array();

foreach($primary_keys as $primary){
$primary_keys_temp[] = $primary['column_name'];
}
$table_primary = $table_primary_temp;

$primary_keys = $primary_keys_temp;
} else {
$table_primary = array( $table_primary[0]['column_name'] );
$primary_keys = array( $primary_keys[0]['column_name'] );
}

} else {
$table_primary = '';
$primary_keys = '';
}


$columns = array();
$foreign_keys = array();

foreach($all_columns as $column_name){
switch(true){
case in_array($column_name, $table_primary):
case in_array($column_name, $primary_keys):
case $column_name == 'created_at':
case $column_name == 'updated_at':
break;
Expand All @@ -102,7 +103,7 @@ private function _controller_generation()
}

$table = $this->_table;
$markers['#TABLE_PHP#'] = function($marker, $template) use ($columns, $foreign_keys, $table_primary, $table){
$markers['#TABLE_PHP#'] = function($marker, $template) use ($columns, $foreign_keys, $primary_keys, $table){

$matches = array();
preg_match_all( '/#TABLE_PHP#(.*?)#END_TABLE_PHP#/s', $template, $matches );
Expand Down
28 changes: 15 additions & 13 deletions classes/generators/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,32 @@ private function _model_generation()
$pdo = DB::connection(Config::get('database.default'))->pdo;
$all_columns = $pdo->query('DESCRIBE ' . $this->_table )->fetchAll( PDO::FETCH_COLUMN );

$table_primary = $pdo->query("SHOW INDEX FROM tasks WHERE Key_name = 'primary'")->fetch( PDO::FETCH_ASSOC );
$primary_keys = $pdo->query("SHOW INDEX FROM {$this->_table} WHERE Key_name = 'primary'")->fetchAll( PDO::FETCH_ASSOC );

if ( count( $table_primary ) > 0){
if ( count( $primary_keys ) > 0){

if (count( $table_primary ) > 1){
$table_primary_temp = array();
foreach($table_primary as $primary){
$table_primary_temp[] = $primary['column_name'];
if (count( $primary_keys ) > 1){
$primary_keys_temp = array();

foreach($primary_keys as $primary){
$primary_keys_temp[] = $primary['column_name'];
}
$table_primary = $table_primary_temp;

$primary_keys = $primary_keys_temp;
} else {
$table_primary = array( $table_primary[0]['column_name'] );
$primary_keys = array( $primary_keys[0]['column_name'] );
}

} else {
$table_primary = '';
$primary_keys = '';
}

$columns = array();
$foreign_keys = array();

foreach($all_columns as $column_name){
switch(true){
case in_array($column_name, $table_primary):
case in_array($column_name, $primary_keys):
case $column_name == 'created_at':
case $column_name == 'updated_at':
break;
Expand All @@ -102,7 +104,7 @@ private function _model_generation()

$table = $this->_table;

$markers['#TABLE_PHP#'] = function($marker, $template) use ($columns, $foreign_keys, $table_primary, $table){
$markers['#TABLE_PHP#'] = function($marker, $template) use ($columns, $foreign_keys, $primary_keys, $table){

$matches = array();
preg_match_all( '/#TABLE_PHP#(.*?)#END_TABLE_PHP#/s', $template, $matches );
Expand Down Expand Up @@ -132,9 +134,9 @@ private function _model_generation()
//set up dummies
$columns = array();
$foreign_keys = array();
$table_primary = array();
$primary_keys = array();
$table = '';
$markers['#TABLE_PHP#'] = function($marker, $template) use ($columns, $foreign_keys, $table_primary, $table){
$markers['#TABLE_PHP#'] = function($marker, $template) use ($columns, $foreign_keys, $primary_keys, $table){

$matches = array();
preg_match_all( '/#TABLE_PHP#(.*?)#END_TABLE_PHP#/s', $template, $matches );
Expand Down
19 changes: 12 additions & 7 deletions templates/model/model.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*
****************/
class #CLASS# extends Eloquent {
#TABLE_PHP#<?php
echo "static::\$table = \"{$table}\";";
?>#END_TABLE_PHP#
#TIMESTAMPS##RELATIONS#
/**
Expand All @@ -15,22 +17,25 @@ class #CLASS# extends Eloquent {
*/
static public function validate($input, $exclude_fields = array(), $ignore_id = 0){
$rules = array(//enter validation rules here
#TABLE_PHP#
<?php
#TABLE_PHP#<?php
foreach( $columns as $column_name ){
echo "
if ($column_name == 'name'){
echo "
'$column_name' => 'required|unique:'.static::\$table.','.$column_name.','.\$ignore_id,";
} else {
echo "
'$column_name' => 'required',";
}
}
?>
#END_TABLE_PHP#
);

$messages = array(//enter validation messages here
#TABLE_PHP#
<?php
#TABLE_PHP#<?php
foreach( $columns as $column_name ){
echo "
'$column_name' => ''";
'$column_name' => '',";
}
?>
#END_TABLE_PHP#
Expand Down

0 comments on commit f8823fa

Please sign in to comment.