Autogenerate model classes with Laravel 4 (also using an existing database with L4)

I developed my database in MySQL Workbench and installed all my foreign keys, etc.

I want to use this database schema with Laravel 4, however, from the documents there is no word about any ability to work with an existing set of database tables. In my opinion, other structures, such as Cake with its "baking", allow you to automatically create model classes based on tables already in your database.

I looked around and didn’t see anything like it for Laravel 4. The closest I found was the Jeffrey Way Generator package for the artisan, however this only creates a basic model and does not detect an established external relationship.

Is this possible with Laravel 4, or will I have to do it all manually?

+4
source share
3 answers

The good news is that Antonio has just finished his MySQL WorkBench in Eloquent ORM converter. This is a great solution, but it is coming to an end, but it can help you a lot.

Update: the link is not working at the moment. The wabpage says: "We're remaking things, we'll be back soon!". I have already sent Antonio an email asking when this service will be available again.

Antonio said he will be back, but there is no approximate time of arrival. We have to wait..

+11
source

cakePHP does a great job of extracting your entire project from the DB schema already in place. Laravel does not currently support anything like this. One of the minor functions still keeps me from accepting laravel.

+1
source

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; /** * just a quick helper to generate model classes * from mysql to Eloquent style ones.. * @author Mario */ class ModelController extends \BaseController { /** * save Classes in folder of choice * * @return void */ 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); } } /** * provide structure indexed by table * * @param type $keys * @return type */ 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)) { //one way $return[$key->TABLE_NAME]['fk'][] = array('column' => $key->COLUMN_NAME, 'refColumn' => $key->REFERENCED_COLUMN_NAME, 'refTable' => $key->REFERENCED_TABLE_NAME,); //and the other $return[$key->REFERENCED_TABLE_NAME]['fk'][] = array('column' => $key->REFERENCED_COLUMN_NAME, 'refColumn' => $key->COLUMN_NAME, 'refTable' => $key->TABLE_NAME,); } } return $return; } } 

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

+1
source

Source: https://habr.com/ru/post/1479992/


All Articles