-
Notifications
You must be signed in to change notification settings - Fork 32
Added a custom repository method for indexation with "meilisearch:import" command #345
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||||||||
<?php | ||||||||||||
|
||||||||||||
declare(strict_types=1); | ||||||||||||
|
||||||||||||
namespace Meilisearch\Bundle\DataProvider; | ||||||||||||
|
||||||||||||
interface DataProvider | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
{ | ||||||||||||
/** | ||||||||||||
* Returns every objects that need to be indexed. | ||||||||||||
* | ||||||||||||
* @param int $limit | ||||||||||||
* @param int $offset | ||||||||||||
* | ||||||||||||
* @return array | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
*/ | ||||||||||||
public function getAll(int $limit = 100, int $offset = 0): array; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
maybe ? :) |
||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Bundle\DataProvider; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
|
||
final class DoctrineOrmDataProvider implements DataProvider | ||
{ | ||
private string $entityClassName; | ||
private ManagerRegistry $managerRegistry; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public function setEntityClassName(string $entityClassName): void | ||
{ | ||
$this->entityClassName = $entityClassName; | ||
} | ||
Comment on lines
+19
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why making this stateful? I'd register this provider as a separate service for each index and pass the class name via constructor |
||
|
||
public function getAll(int $limit = 100, int $offset = 0): array | ||
{ | ||
if (empty($this->entityClassName)) { | ||
throw new \Exception('No entity class name set on data provider.'); | ||
} | ||
|
||
$manager = $this->managerRegistry->getManagerForClass($this->entityClassName); | ||
$classMetadata = $manager->getClassMetadata($this->entityClassName); | ||
$entityIdentifiers = $classMetadata->getIdentifierFieldNames(); | ||
$repository = $manager->getRepository($this->entityClassName); | ||
$sortByAttrs = array_combine($entityIdentifiers, array_fill(0, \count($entityIdentifiers), 'ASC')); | ||
|
||
return $repository->findBy( | ||
[], | ||
$sortByAttrs, | ||
$limit, | ||
$offset | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,10 @@ public function getConfigTreeBuilder(): TreeBuilder | |
->info('Property accessor path (like method or property name) used to decide if an entry should be indexed.') | ||
->defaultNull() | ||
->end() | ||
->scalarNode('data_provider') | ||
->info('Method of the entity repository called when the meilisearch:import command is invoked.') | ||
->defaultNull() | ||
->end() | ||
Comment on lines
+62
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should introduce something like:
|
||
->arrayNode('settings') | ||
->info('Configure indices settings, see: https://www.meilisearch.com/docs/reference/api/settings') | ||
->beforeNormalization() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Bundle\Tests\DataProvider; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Meilisearch\Bundle\DataProvider\DataProvider; | ||
use Meilisearch\Bundle\Tests\Entity\Ticket; | ||
|
||
final class TicketDataProvider implements DataProvider | ||
{ | ||
private ManagerRegistry $managerRegistry; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public function getAll(int $limit = 100, int $offset = 0): array | ||
{ | ||
$manager = $this->managerRegistry->getManagerForClass(Ticket::class); | ||
$repository = $manager->getRepository(Ticket::class); | ||
|
||
return $repository->findBy( | ||
['sold' => false], | ||
['id' => 'ASC'], | ||
$limit, | ||
$offset | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Bundle\Tests\Entity; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
#[ORM\Entity] | ||
class Ticket | ||
{ | ||
/** | ||
* @ORM\Id | ||
* | ||
* @ORM\Column(type="integer") | ||
*/ | ||
#[ORM\Id] | ||
#[ORM\Column(type: Types::INTEGER)] | ||
private int $id; | ||
|
||
/** | ||
* @ORM\Column(type="string") | ||
*/ | ||
#[ORM\Column(type: Types::STRING)] | ||
private string $barcode; | ||
|
||
/** | ||
* @ORM\Column(type="boolean", options={"default"=false}) | ||
*/ | ||
#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])] | ||
private bool $sold; | ||
|
||
/** | ||
* @param int $id | ||
* @param string $barcode | ||
* @param bool $sold | ||
*/ | ||
public function __construct(int $id, string $barcode, bool $sold) | ||
{ | ||
$this->id = $id; | ||
$this->barcode = $barcode; | ||
$this->sold = $sold; | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(int $id): Ticket | ||
{ | ||
$this->id = $id; | ||
|
||
return $this; | ||
} | ||
|
||
public function getBarcode(): string | ||
{ | ||
return $this->barcode; | ||
} | ||
|
||
public function setBarcode(string $barcode): Ticket | ||
{ | ||
$this->barcode = $barcode; | ||
|
||
return $this; | ||
} | ||
|
||
public function isSold(): bool | ||
{ | ||
return $this->sold; | ||
} | ||
|
||
public function setSold(bool $sold): Ticket | ||
{ | ||
$this->sold = $sold; | ||
|
||
return $this; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use ServiceLocator from Symfony instead of coupling this to the SearchService..