Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom engines (eg for mongodb) #1294

Closed
pimlie opened this issue Jul 27, 2017 · 3 comments
Closed

Support for custom engines (eg for mongodb) #1294

pimlie opened this issue Jul 27, 2017 · 3 comments
Labels

Comments

@pimlie
Copy link
Contributor

pimlie commented Jul 27, 2017

For my project I am using mongodb through laravel-mongodb. Unfortunately I couldnt get laravel-datatables to work with mongodb, as laravel-mongodb requires different syntax in the QueryBuilderEngine as it does not use sql.

To still use mongodb I've written a rough mongodb engine for datatables. But as it seems at the moment there is no easy way of adding a custom engine to datatables, I had to completely exend the laravel-datatables project and e.g. overwrite all methods where configuration settings are pulled.

As this implementation is very ugly and difficult to maintain, do you have any recommendations how to better add a custom engine for mongodb?

@yajra
Copy link
Owner

yajra commented Jul 29, 2017

On version 8, I am also trying to create an engine for Laravel Scout. See https://github.com/yajra/laravel-datatables-scout for reference for possible implementation.

@yajra yajra added the question label Jul 29, 2017
@pimlie
Copy link
Contributor Author

pimlie commented Jul 29, 2017

Will you be changing the general engine interface in v8 as well, like removing the eloquent and queryBuilder methods in Datatables.php? From a code perspective I think it would be best to keep the interfaces for built-in engines and custom engines as much the same as possible.

One way to keep the interface more uniform would be by adding a magic __call/__callStatic method like the following pseudo code (basic principle is the same as laravel's Illuminate/Support/Traits/Macroable.php):

// Datatables.php
public function __call($method, $parameters)
{
  $engines = $config->get('datatables.engines');
  if (!isset($engines[$method])) {
    throw new BadMethodCallException("Method {$method} does not exist.");
  }

  return $engines[$method]::getInstance($parameters);
}

Then you would only need to add your engine to the config.engines (and builder) array and you could just do return Datatables::scout($model); and/or return Datatables::of($scoutModel);.

Only difficulty with this solution i see currently is that e.g. jenssegers/mongodb extends the Eloquent classes, so the of method in Datatables.php would probably return an Eloquent engine and not a Moloquent engine. Either people should be instructed to add their custom engines definition before the eloquent definition (in config/datatables.php) or we should add a check for these kind of inheritances between engines on startup.

@yajra
Copy link
Owner

yajra commented Jul 29, 2017

Will you be changing the general engine interface in v8 as well, like removing the eloquent and queryBuilder methods in Datatables.php?

The interface would remain the same. The only major breaking change is the namespace and class name being updated from Datatables to DataTables.

I was also thinking on how to implement return Datatables::scout($model); assuming that scout is on a separate package. Will try your suggestion when I got the chance. Thanks!

@pimlie pimlie closed this as completed Dec 11, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Oct 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants