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

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

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

kayrunm
Copy link
Contributor

@kayrunm kayrunm commented Nov 11, 2021

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
Copy link

+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
Copy link

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
Copy link

@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
Copy link
Contributor Author

kayrunm commented May 26, 2022

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

@CraigMonva
Copy link

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

@JustSteveKing JustSteveKing merged commit 381d8bc into tighten:master Jun 21, 2022
@JustSteveKing
Copy link
Collaborator

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

@kayrunm
Copy link
Contributor Author

kayrunm commented Jun 21, 2022

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

No worries at all! Happy to have contributed 🔥

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

Successfully merging this pull request may close these issues.

4 participants