PHP Agent - Alias for tables via Doctrine

Hi,

We’re in the process of migrating from the v1.* to v2 for the php / laravel agent.

We have some tables named in a way that don’t adhere to the default doctrine schemas, e.g.

 Doctrine\DBAL\Schema\Exception\TableDoesNotExist 
 There is no table with name "artist_tag_gig_template" in the schema

The actual table name is artist_tag_gigtemplate

Is there a way to annotate this table via the php agent in the same way you can alias a table in Laravel (e.g. protected $table= “artist_tag_gigtemplate” on the Model).

class GigTemplateArtistTag extends Model
{
    use HasFactory;

    public $table = 'artist_tag_gigtemplate';

...

Not a huge issue as we can migrate and rename the tables but would rather minimise the live service impact; and admittedly this is our issue with the way tables are named.

Hi,
Did you try to rename your collection with this method?
Just to understand, and improve our documentation, do you have some feedback about it ? I ask the question because if you ask the question on the forum, there is a probably an issue with our documentation :slight_smile:

Ah no sorry you’re right - might just be “users being users” and not seeing it - but I probably got distracted going through Migrations > https://docs.forestadmin.com/developer-guide-agents-php/getting-started/migrating/steps

Then clicking Getting Started - PHP Developer Guide

And following the ‘Previous/Next’ buttons at the bottom - these don’t appear to follow the same headings as the left menu (e.g. left menu goes Getting started, collection selection, naming conflicts) but the quicklinks at the bottom go straight into Agent Customisation.

So I probably followed the quicklinks at the bottom and not the left submenu, as well as searching for terms like ‘table alias php agent’ or ‘table rename’ not returning that page.

1 Like

Collection rename is fine though - so just me calling collections ‘tables’

h

1 Like

Thank you for the feedback. I really appreciate it :slight_smile:

1 Like

Does this work with EloquentDatasources in terms of the table error?

I’ve tried the below as part of the generated forest_admin config, but the examples suggest to pass in the Model rather than any reference to the underlying table?

I’ve tried variations of the below using the table name, but also the Model just in case (Model name is GigTemplateArtistTag) but it still throws the TableDoesNotExist exception.


use ForestAdmin\AgentPHP\Agent\Builder\AgentFactory;
use ForestAdmin\AgentPHP\DatasourceEloquent\EloquentDatasource;

return static function () {
    app()->make(AgentFactory::class)->addDatasource(
        new EloquentDatasource(
            [
                'driver'   => env('DB_CONNECTION'),
                'host'     => env('DB_HOST'),
                'port'     => env('DB_PORT'),
                'database' => env('DB_DATABASE'),
                'username' => env('DB_USERNAME'),
                'password' => env('DB_PASSWORD'),
                // OR
                // 'url' => env('DATABASE_URL'),
            ]
        ),
        [
            'rename' => [
                'artist_tag_gig_template' => 'artist_tag_gigtemplate'
            ],
        ]
    );


};

The table names are derived from the Relationships rather than the table themselves, so manually specified the table name in the eloquent relationships themselves in the model.

Hi,

Have you defined the $table attribute in your model, or is this just an example ?

Yes the $table is defined already on the Model, but Doctrine doesn’t appear to use that at all. We’ve just got around it renaming our tables at database level and defining foreign table names on relationships where we can.

What is your version of Laravel ?

8.54

Writing something here to get past char limit.

Hi, I’m not able to reproduce the problem on my side.

I will continue my research and let you know when I have found a solution.