michaelbaril / sqlout

MySQL driver for Laravel Scout
42 stars 6 forks source link

morphMap Issue #1

Closed sineld closed 5 years ago

sineld commented 5 years ago

Using,

        Relation::morphMap([
            'App\Models\Profile',
            'App\Models\User',
        ]);

in AppServiceProvider breaks application. I'm not sure if it is related to your package. Please check.

michaelbaril commented 5 years ago

Hi, I would need more information to understand the context. Could you provide me with the error message and stack trace? Thanks

sineld commented 5 years ago

Let me explain, when using Polymorhic relations, Laravel writes the whole Model with the namespace (App\User), but when we add the morphMap to AppServiceProvider like below:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        //
    }

    public function boot()
    {
        Relation::morphMap([
            'App\User',
        ]);
    }
}

App\User becomes users and this occurs error below:

/Users/sineld/Code/laravel58/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
    public function getMorphClass()
    {
        $morphMap = Relation::morphMap();

        if (! empty($morphMap) && in_array(static::class, $morphMap)) {
            return array_search(static::class, $morphMap, true);
        }

        return static::class;
    }

    /**
     * Create a new model instance for a related model.
     *
     * @param  string  $class
     * @return mixed
     */
    protected function newRelatedInstance($class)
    {
        return tap(new $class, function ($instance) {
            if (! $instance->getConnectionName()) {
                $instance->setConnection($this->connection);
            }
        });
    }

    /**
     * Get all the loaded relations for the instance.
     *
     * @return array
     */
    public function getRelations()
    {
        return $this->relations;
    }

    /**
     * Get a specified relationship.
     *
     * @param  string  $relation
Arguments
"Class 'users' not found"

Please see images attached. 2019-10-03 22-27-28 (MySQL 5 7 27) localhost homestead searchindex (2) 2019-10-03 22-37-03 Whoops! There was an error

michaelbaril commented 5 years ago

Indeed, it's a bug. It should be fixed with the latest commit on master, please check. By the way, you should do a full reindex after you've changed the morph map.

sineld commented 5 years ago

Yes, it's solved. Thanks. You may close the issue.

michaelbaril commented 5 years ago

Thanks for your feedback. I've included the fix in the latest release.