G
G
great_772020-02-09 04:55:45
symfony
great_77, 2020-02-09 04:55:45

How to solve the error You have requested a non-existent service "App\Entity\Song"?

Symphony 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);
    	}
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
great_77, 2020-02-09
@great_77

$song = $this->getContainer()
->get('doctrine')
->getRepository(Song::class)
->findAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question