0

хочу написать команду, которая парсит из json и заносит в mysql сущность, и вот после получения данных происходит такой метод:

   protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $books = $this->get_decoded_json($output);
        if (!$books) {
            return Command::FAILURE;
        }

        $em = $this->entityManager;

        $authors = array();
        foreach($books as $id=>$book) {
            $dbBook = new Book();

            $output->writeln("[$id][Title] $book->title");

            $dbBook->setTitle($book->title);
            $dbBook->setIsbn($book->isbn);
            $dbBook->setPageCount($book->pageCount);
            $dbBook->setStatus($book->status);

            // $dbBook->setPublishedDate($book->pageCount);
            // $dbBook->setThumbnailUrl($book->pageCount);
            // $dbBook->setShortDescription($book->pageCount);
            // $dbBook->setLongDescription($book->pageCount);

            // $this->log($output, $id, $book, 'publishedDate');
            $this->log($output, $id, $book, 'shortDescription');
            $this->log($output, $id, $book, 'longDescription');
            $this->log($output, $id, $book, 'thumbnailUrl');
            //$output->writeln($id.': '.$book->authors);
            //$output->writeln($id.': '.$book->categories);
            //$output->writeln("");
            $em->persist($dbBook);
        }
        $em->flush();

на что консоль отвечает ошибкой: Call to a member function persist() on null

3
  • приложите код всего класса вместе с внедрением Entity Manager'а. Именно там и закралась ошибка 25 июл 2022 в 15:16
  • docs.google.com/document/d/… - вот в гугл документ положила весь класс, не знаю как еще его уместить 25 июл 2022 в 15:27
  • да, теперь там ошибка с подключением EntityManager: Cannot autowire service "App\Command\JsonParserCommand": argument "$entityManager" of method "__construct()" references class "Doctrine\ORM\EntityManager " but no such service exists. Try changing the type-hint to "Doctrine\ORM\EntityManagerInterface" instead. 25 июл 2022 в 15:43

1 ответ 1

1

$em у вас со значением null, о чём и говорит ошибка. А узнать то, откуда "ноги растут" придётся воспользоваться отладкой. См. соответствующую тему:

https://ru.stackoverflow.com/a/701146/191482

а также во время отладки смотреть стэк вызова

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.