ForestAdmin / laravel-forestadmin

🌱 Laravel Agent for Forest Admin
GNU General Public License v3.0
73 stars 16 forks source link

No collections are detected #89

Open jeffladiray opened 3 weeks ago

jeffladiray commented 3 weeks ago

Expected behavior

I want to see all of my collections

Actual behavior

No collections are detected. .forestadmin-schema.json contains "collections": [], even so my database and laravel models are correctly defined and initialized in my database.

My current code is quite similar to the one mentionned in the documentation.

My schema is still sent out to forest admin servers, and the frontend seems to be working as expected.

Failure Logs

None. Even switching to loggerLevel "Debug" doesn't share more information - Outside of the Schema not updated log message.

Context

(CC @mngarwood)

nicolasalexandre9 commented 3 weeks ago

Hello @mngarwood,

Are the models in the app namespace ?

mngarwood commented 3 weeks ago

There's only one model in the branch where I have the agent installed (User), and I just confirmed it is in the app namespace. @nicolasalexandre9

mngarwood commented 3 weeks ago

I might be off base here, but is it possible that my User model isn't being picked up because it extends Illuminate\Foundation\Auth\User (which extends Model) instead of Illuminate\Database\Eloquent\Model directly?

I found this— "The Eloquent data source allows importing collections from all models class that extends the abstract class Illuminate\Database\Eloquent\Model." —in the PHP Developer Guide here.

jeffladiray commented 3 weeks ago

According to this doc from Laravel 11.x, User should extend Illuminate\Database\Eloquent\Model, so my understanding is that is should work as expected here as well, but that's worth trying.

Do you have a model that extended Illuminate\Database\Eloquent\Model directly just to give it a try ?

mngarwood commented 3 weeks ago

@jeffladiray Correct! I tried it on a different branch with some more models (extending Illuminate\Database\Eloquent\Model directly) and still no dice.

mngarwood commented 2 weeks ago

I figured out how to solve the problem for my development environment! The BaseDatasource class was not configured to accept a 'port' parameter for the database connection, and mine is operating on a different port than the standard 5432.

In the makeDoctrineConnection method of BaseDatasource, I changed the $config array like so:

if ($databaseConfig['driver'] === 'sqlite') {
            $config = [
                'path'      => $databaseConfig['database'],
            ];
        } else {
            $config = [
                'user'      => $databaseConfig['username'],
                'password'  => $databaseConfig['password'],
                'host'      => $databaseConfig['host'],
                'dbname'    => $databaseConfig['database'],
                'port'      => $databaseConfig['port'],  // this is the line I added
            ];
        }
mngarwood commented 2 weeks ago

Sidenote - I also had to change something else in AgentFactory to get model editing to work for my dev environment.

private function buildCache(): void
    {
        $filesystem = new Filesystem();
        $directory = $this->config['cacheDir'];
        self::$container->set('cache', new CacheServices($filesystem, $directory));

        if ($this->hasEnvSecret) {
            $serializableConfig = $this->config;
            unset($serializableConfig['logger'], $serializableConfig['customizeErrorMessage']);
            self::$container->get('cache')->add('config', $serializableConfig, self::TTL_CONFIG);
        }
    }

I kept getting errors about a closure not being serializable and tracked it down to my custom 'logger' function in my config/forest.php config file. Copied this bit from another function earlier in the AgentFactory class to solve that problem, now all is well!

matthv commented 2 weeks ago

Nice catch about the missing port attribute.

I've just fixed it in the latest version v1.10.1. https://github.com/ForestAdmin/agent-php/pull/111

About the second topic on AgentFactory, we have another branch that improves performance and revises the cache system. It's currently under development and should be merged soon. We'll keep you posted.