ArtisanTinkerer.github.io

Blog

View on GitHub

Datatables One to Many Joins

Datatables

I’m trying to add multiple data_sources to a widget, it looks like Mjoin will work.

Laravel uses link tables, in my case it’s actually many to many.

like this:

->join(
        Mjoin::inst( 'access' )
            ->link( ... )
            ->order( ... )
            ->fields( ... )
    )

** example ** staff -> staff_access -> access

** mine ** widgets -> data_source_widget -> data_sources

like this:

 ->join(
                Mjoin::inst( 'data_sources' )//table to link to
                ->link('widgets.id','data_source_widget.widget_id' ) //left table id, name in link table
                ->link('data_sources.id','data_source_widget.data_source_id' ) //right table id, name in link table

                ->order('data_sources.description asc')// order of joined data - optional
                ->fields(
                    Field::inst( 'id' ) //first field read from joined table
                    ->options( 'data_sources', 'id', 'description' )
                        ->validator( 'Validate::notEmpty' ),
                    Field::inst( 'description' ) //second field read from joined table
                )
            )
            ->process($postData)
            ->json();

I now have data_sources in the JSON.

Client Side

Add this to columns:

         { data: "access", render: "[, ].name" }

Now I need a multi select-on the Editor modal - with all the possible data source options, surely this is just the same as using a select with a foreign key.