artvys/cake-search

CakePHP adapter for artvys/search package

1.0.0 2023-05-10 15:54 UTC

This package is auto-updated.

Last update: 2024-05-10 18:01:16 UTC


README

This package is CakePHP adapter for the package artvys/search. It provides few boilerplate classes to make integration with framework breeze.

1. Installation

This section describes installation of this package. If you have composer installed, then simply run:

composer require artvys/cake-search

You don't need to install the core package separately. It will be installed automatically.

2. Implementing your TableSearchSource

This package provides you base class TableSearchSource that includes boilerplate code for searching in Table classes from CakePHP.

To use it, you need to extend it and implement 3 abstract methods:

use App\Model\Entity\Article;
use Artvys\Search\Cake\Engines\Compiled\SearchSources\Table\TableSearchSource;

class ArticlesTableSearchSource extends TableSearchSource {
    protected function table(): Table {
        return TableRegistry::getTableLocator()->get('Articles');
    }

    protected function fields(SearchFieldBuilder $builder, CompiledQuery $query, int $limit): void {
        $builder->add(Field::contains('title'))
            ->add(Field::contains('body'));
    }

    protected function makeResultMapper(): callable {
        return fn(Article $a) => SearchResult::make($a->title, $a->body, Router::url(['controller' => 'Articles', 'action' => 'edit']));
    }
}

Those methods do 3 simple things. You need to specify which Table will be searched, which columns will be used and how to convert instances of entities to SearchResult. Please note that you don't need to use TableRegistry or Router directly. You can inject your dependencies and then just return them in those required methods.

Take look through the base classes. They contain a lot of little methods meant for extension and can prove to be quite handy for a lot of common use cases.

3. Where to go from here?

The rest of the documentation can be found in the core package artvys/search.