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

Configurable Model class name #16

Open
tansautn opened this issue Dec 2, 2024 · 1 comment
Open

Configurable Model class name #16

tansautn opened this issue Dec 2, 2024 · 1 comment

Comments

@tansautn
Copy link

tansautn commented Dec 2, 2024

Some times, Developers used this package need to override default model or extend it.

So used hardcoded model name in Morph relations will break.

Here is example in my case:

File : app/Models/Post.php

class Post extends \TomatoPHP\FilamentCms\Models\Post implements HasMedia
{
    use InteractsWithMedia;

    protected $with = ['postMeta', 'media', 'categories', 'comments', 'author', 'tags'];

    public function next()
    {
        return static::where('id', '>', $this->id)->where('is_published', true)->where('type', '=', $this->type)->orderBy('id', 'asc')->first();
    }

    public function previous()
    {
        return static::where('id', '<', $this->id)->where('is_published', true)->where('type', '=', $this->type)->orderBy('id', 'desc')->first();
    }

    // just demo extended case
    public function scopePublished(Builder $query): Builder
    {
        return $query->whereNotNull('published_at');
    }
}

Then when Morph query builder load media, comments query will like this one:

select count(*) as aggregate from `comments` where `comments`.`content_type` = 'App\\Models\\Post' and `comments`.`content_id` = 10 and `comments`.`content_id` is not null

But actual value in DB is 'TomatoPHP\\FilamentCms\\Models\\Post' .

Currently. I must used workaround hack: reupdate value when post created and update. But when comment added. Post is remain unchanged

namespace App\Listeners;

use TomatoPHP\FilamentCms\Events\PostCreated;
use TomatoPHP\FilamentCms\Events\PostUpdated;
use Illuminate\Support\Facades\DB;

class UpdatePostMediaModelType
{
    /**
     * Handle the event.
     */
    public function handle(PostCreated|PostUpdated $event): void
    {
        // Update media model_type for the specific post
        DB::table('media')
            ->where('model_type', 'TomatoPHP\FilamentCms\Models\Post')
//            ->where('model_id', $event->post->id)
            ->update(['model_type' => 'App\Models\Post']);

        DB::table('comments')
          ->where('model_type', 'TomatoPHP\FilamentCms\Models\Post')
//            ->where('model_id', $event->post->id)
          ->update(['model_type' => 'App\Models\Post']);
    }
}
Copy link

linear bot commented Dec 2, 2024

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

1 participant