tighten / parental

Use single table inheritance in your Laravel app
MIT License
1.36k stars 98 forks source link

Type column not being set #45

Closed tikkibar closed 1 year ago

tikkibar commented 5 years ago

I've created a simple project to test the functionality of parental. However, the type column is not being set when creating a child model. It is always set to null ('type' is set for fillable). If I manually pass a type when creating the model it'll be set. I shouldn't need to do that, right? It will determine the type when the model is created? What might I be doing wrong?

dallincoons commented 5 years ago

It should be setting the type column (https://github.com/tightenco/parental/blob/master/tests/Features/TypeColumnGetsSetAutomatically.php#L14). Can you maybe show some of your code?

tikkibar commented 5 years ago

It's very simple:

Person: `namespace App\Models;

use Illuminate\Database\Eloquent\Model; use \Tightenco\Parental\HasChildren;

class Person extends Model { use HasChildren;

protected $table = 'people';

protected $fillable = ['person_type', 'first_name'];

protected $childColumn = 'person_type';

protected $guarded = [];

protected $childTypes = [
    'man'   => Man::class,
    'woman' => Woman::class
];

}`

Man: `<?php

namespace App\Models;

use \Tightenco\Parental\HasParent;

class Man extends Person { use HasParent; }`

Woman:

`namespace App\Models;

use \Tightenco\Parental\HasParent;

class Woman extends Person { use HasParent; }`

PersonController:

`namespace App\Controllers;

use App\Models\Person; use App\Models\Man; use App\Models\Woman;

use Slim\Http\Request; use Slim\Http\Response;

class PersonController extends Controller { public function index(Request $request, Response $response, array $args) { Man::create(['first_name' => 'Jack']); Woman::create(['first_name' => 'Jill']);

    $people = Person::all();

    $payload = [];
    foreach ($people as $person) {
        $payload[$person->id] = $person->getAttributes();
    }

    return $response->withStatus(200)->write(json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}

}`

tikkibar commented 5 years ago

After some debugging it appears that the callback for the creating event is not getting registered because the dispatcher is null. How and where does the event dispatcher get set?

tikkibar commented 5 years ago

I'm using standalone eloquent. And, according to the discussion on this page:

https://laracasts.com/discuss/channels/eloquent/how-to-use-events-with-standalone-eloquent?page=0

The event dispatcher must be setup for the model when using standalone eloquent.

tikkibar commented 5 years ago

Got it working..

The fix is to require illuminate/events in the composer.json file.

In HasParent.php: Add: use Illuminate\Events\Dispatcher;

In bootHasParent() add at the top of the function:

  if (static::getEventDispatcher() === null) {
      static::setEventDispatcher(new Dispatcher());
  }

OR

Outside of the trait you can do: $capsule->setEventDispatcher(new Dispatcher());

ckonkel commented 5 years ago

Seeing the same issue, but have full laravel and nova installed...

ckonkel commented 5 years ago

Seeing the same issue, but have full laravel and nova installed...

Turns out I did not have use HasChildren; set in the parent - solved the issue.

mattstauffer commented 1 year ago

Hey friends! The team at Tighten has taken Parental back over and we're re-opening the issue tracker, but to give us a clean slate, I'm going to close all old issues that remain from when Caleb closed the issue tracker in 2020. If this is still an outstanding concern with the latest version of Parental, please feel free to open a new issue referencing this one.

Thank you!