tighten / parental

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

Allow child types to be set via a method. #99

Closed kayrunm closed 2 years ago

kayrunm commented 2 years ago

Similar to #53, this allows the child types to be set via the getChildTypes method.

In our application's use case, once again similar to #53, we use an enum to map the type to the model's class. We have a getClassMap() static method on our enum which would reduce the amount of duplicate code if we were able to do this.

CraigMonva commented 2 years ago

+1 for this issue with using php 8.1 enums as the $childTypes identifiers.

PHP 8.0 using class constants was fine.

class ProductType
{
    public const LOAN = 1;
    public const CREDIT_CARD = 2;
}

class ProductResult extends Model
{
    use HasChildren;

    protected $childColumn = 'product_type';

    protected $childTypes = [
        ProductType::LOAN        => LoanResult::class,
        ProductType::CREDIT_CARD => CreditCardResult::class,
    ];

    protected $casts = [
        'product_type' => ProductType::class,
    ];
}

Convering old style class/constant based enums to real php 8.1 enums results in Fatal error:

PHP Fatal error:  Constant expression contains invalid operations in /var/www/html/app/Models/Results/ProductResult.php
enum ProductType: int
{
    case CREDIT_CARD = 1;
    case LOAN = 2;
}

class ProductResult extends Model
{
    use HasChildren;

    protected $childColumn = 'product_type';

    protected $childTypes = [
        ProductType::LOAN->value        => LoanResult::class,
        ProductType::CREDIT_CARD->value => CreditCardResult::class,
    ];

    protected $casts = [
        'product_type' => ProductType::class,
    ];
}
HassanZahirnia commented 2 years ago

Are you guys still using this package ? I'm a bit hesitant to use it since there doesn't seem to be a lot of active development going on, like take this PR for instance, no response from the owner yet and it concerns me.

Let me know if anyone knows a better package or a solution to achieve same result using pure laravel/php

CraigMonva commented 2 years ago

@HassanZahirnia

Yeah still using the package. Had to hardcode the $childTypes rather than use the Enum value. Defo a shame its not being maintained at the moment.

kayrunm commented 2 years ago

@calebporzio @JustSteveKing any potential for movement on this? Sorry for the tag.

CraigMonva commented 2 years ago

@calebporzio @JustSteveKing - Tagging again to hope for some movement/feedback on this.

JustSteveKing commented 2 years ago

Sorry this has taken a while! Will get a release ready tomorrow 🙂

kayrunm commented 2 years ago

Sorry this has taken a while! Will get a release ready tomorrow 🙂

No worries at all! Happy to have contributed 🔥