Symfony 4.4:
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use App\Entity\Song;
class ImportSongsDataToSongIndex extends ContainerAwareCommand
{
protected static $defaultName = 'app:import-songs-data-to-elastic';
protected function configure()
{
$this
->setDescription(
'Importe songs'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Importing songs in the index');
$songs = $this->getSongs();
$this->createSongDocument($songs);
}
private function getSongs()
{
$song = $this->getContainer()
->get(Song::class)
->findAll();
}
private function createSongDocument(array $songs)
{
$documents = [];
foreach($songs as $song) {
// Create a document
$track = [
'id' => $song->getId(),
'name' => $song->getName(),
'description' => $song->getDescription(),
'name_not_analyzed' => $name
];
// First parameter is the id of document.
$documents[] = new \Elastica\Document($id, $track);
}
}
}