Hmm, I had the same problem and I wrote a little script myself which generates base classes and solves foreign key problems. This is the main solution and defines the "hasOne" relationship, which you may have to change to hasMany later. I used the controller and create my code template in the view:
Controller:
namespace Admin; class ModelController extends \BaseController { public function create($folder) { $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'UR_SCHEMA'"; $tables = \DB::select($sql); $sql2 = "select * from information_schema.`KEY_COLUMN_USAGE` where constraint_schema = 'UR_SCHEMA' order by table_name"; $keys = \DB::select($sql2); $meta = $this->sortOutMetadata($keys); foreach ($tables as $table) { $metaData = null; if(!empty($meta[$table->TABLE_NAME])){ $metaData = $meta[$table->TABLE_NAME]; } $code = \View::make('model.start', array('table' => $table, 'meta' => $metaData))->render(); file_put_contents($folder.DIRECTORY_SEPARATOR.ucfirst(camel_case($table->TABLE_NAME).'.php'), $code); } } private function sortOutMetadata($keys) { $return = array(); foreach ($keys as $key) { if ($key->CONSTRAINT_NAME == 'PRIMARY') { $return[$key->TABLE_NAME]['pk'] = $key->COLUMN_NAME; } elseif (!empty($key->REFERENCED_TABLE_NAME)) {
My presentation template (pretty much my template template)
<?php echo '<?php'; ?> namespace Model\Base; use Model\Model; class <?php echo ucfirst(camel_case($table->TABLE_NAME));?> extends Model { /** * @var String */ protected $table = '<?php echo $table->TABLE_NAME;?>'; <?php if (isset($meta['pk'])):?> /** * @var String */ protected $primaryKey = '<?php echo $meta['pk'];?>'; /** * attributes not writable from outside * @var mixed */ protected $guarded = array('<?php echo $meta['pk'];?>'); <?php endif;?> /** * Timestamps we dont want here * @var Boolean */ public $timestamps = false; <?php if (isset($meta['fk'])):?> <?php foreach($meta['fk'] as $keys):?> /** * @return HasOne */ public function <?php echo camel_case($keys['refTable']);?>() { return $this->hasOne('Model\<?php echo ucfirst(camel_case($keys['refTable']));?>', '<?php echo $keys['refColumn'];?>', '<?php echo $keys['column'];?>'); } <?php endforeach;?> <?php endif;?> }
Then just create the base classes by pointing it to the folder: (wherever you choose)
$controller = new \Admin\ModelController(); $controller->create(__DIR__ . DIRECTORY_SEPARATOR . 'tmpModel');
This gave me a decent way to get my Auto base classes created as I needed. Remember that you need to see the information_schema schema with your db user.
Hope this helps
source share