Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self referencing foreign keys not generating valid models #12

Open
ghost opened this issue Feb 19, 2016 · 0 comments
Open

Self referencing foreign keys not generating valid models #12

ghost opened this issue Feb 19, 2016 · 0 comments

Comments

@ghost
Copy link

ghost commented Feb 19, 2016

After running the following migration, I get an invalid model generated.
I suggest the issue is in the looping logic in User11001\EloquentModelGenerator\Console\GenerateModelsCommand::addManyToManyRules()
Migration
`
Schema::create('boma_coa_categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
$table->softDeletes();
});

    Schema::create('boma_coa_codes', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('boma_account_code');
        $table->string('boma_account_name');
        $table->text('description');
        $table->string('boma_usage_type');
        $table->string('boma_account_header_code');
        $table->string('boma_account_header_name');
        $table->integer('parent_boma_coa_code_id')->unsigned();
        $table->integer('boma_coa_category_id')->unsigned();
        $table->string('boma_sorting');
        $table->string('boma_data_source');
        $table->string('version_num');
        $table->string('data_source');
        $table->timestamps();
        $table->softDeletes();
    });
    Schema::table('boma_coa_codes', function (Blueprint $table) {
        $table->foreign('boma_coa_category_id')->references('id')->on('boma_coa_categories');
        $table->foreign('parent_boma_coa_code_id')->references('id')->on('boma_coa_codes');
    });`

How I generate Models
php artisan models:generate --path="app/Models/ModelsBase" --overwrite --namespace="App\Models\ModelsBase"

Resulting model class
`<?php namespace App\Models\ModelsBase;

use Illuminate\Database\Eloquent\Model;

class BomaCoaCategory extends Model {
/**
* Generated
*/
protected $table = 'boma_coa_categories';
protected $fillable = ['id', 'name', 'description', 'deleted_at'];
public function bomaCoaCodes() {
return $this->belongsToMany(\App\Models\ModelsBase\BomaCoaCode::class, 'boma_coa_codes', 'boma_coa_category_id', 'parent_boma_coa_code_id');
}

// SHOULD BE
// public function bomaCoaCategory() {
// return $this->hasMany(\App\Models\ModelsBase\BomaCoaCategory::class, 'boma_coa_category_id', 'id');
//}
public function bomaCoaCodes() {
return $this->hasMany(\App\Models\ModelsBase\BomaCoaCode::class, 'boma_coa_category_id', 'id');
}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants