squire icon indicating copy to clipboard operation
squire copied to clipboard

Unable to locate Squire source for [App\Models\Country]

Open ShamarKellman opened this issue 3 years ago • 6 comments

Describe the bug Unable to find source when using extended Country model.

To reproduce

namespace App\Models;

use Squire\Models\Country as SquireCountry;

class Country extends SquireCountry
{
    public function users()
    {
        return $this->hasMany(User::class);
    }
}

Call

App\Models\Country::all();

Expected behavior Expected to show all countries

Context

  • Squire version: 2.0.6
  • Laravel version: 8.33.1
  • Server OS: osx - laravel valet
  • PHP version: 8.0

Additional details Add any other details about the problem here.

ShamarKellman avatar Mar 22 '21 11:03 ShamarKellman

Hey, I cannot replicate this. Please provide a repo where I can, and I will happily reopen this issue. Thanks!

danharrin avatar Apr 26 '21 21:04 danharrin

I face the same issue as @ShamarKellman and I solved it by re-registering sources:

  • SquireServiceProvider.php
<?php

namespace App\Providers;

use App\Models\Country;
use Squire\Repository;
use Illuminate\Support\ServiceProvider;

class SquireServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Repository::registerSource(Country::class, 'ar', resource_path('/squire-data/countries-ar.csv'));
        Repository::registerSource(Country::class, 'fr', base_path('/vendor/squirephp/countries-fr/resources/data.csv'));
        Repository::registerSource(Country::class, 'en', base_path('/vendor/squirephp/countries-en/resources/data.csv'));
    }
}

and

  • register the service provider in config/app.php:
<?php

return [
    // ...
    'providers' => [
        // ...
        App\Providers\SquireServiceProvider::class,
    ],
    // ...
];

aminetiyal avatar Oct 19 '21 12:10 aminetiyal

Ah interesting. I see why this is a bug now. Because the sources are registered to the base class and not the original class, it can't find them. I'm going to think of a good way to fix this.

danharrin avatar Oct 20 '21 08:10 danharrin

I have the same issue

a21ns1g4ts avatar Oct 23 '21 07:10 a21ns1g4ts

I have the same issue

AlexanderFalkenberg avatar Jan 17 '22 09:01 AlexanderFalkenberg

@danharrin - Probably the easiest way to come about this is to refactor the models to require a static getSourceKey() method which is used to register it.

We could always check if the name is an existing class and then recurse its parents, that just seems like too much going on though.

aidan-casey avatar Jan 20 '22 15:01 aidan-casey