When outputting the code, an error occurs:

Attempted to call the "Doctrine \ Bundle \ DoctrineBundle \ Registry" called "persist" of class.

Rummaged could not find a solution. Here is my code:

protected function execute(InputInterface $input, OutputInterface $output) { $client = new Client(); // Go to the booking.com website $crawler = $client->request('GET', 'http://www.booking.com/country.en-gb.html'); $crawler = $crawler->filter('body#b2countryPage > div#bodyconstraint > div#bodyconstraint-inner > div.lp_flexible_layout_content_wrapper > div#countryTmpl > div.block_third > div.block_header'); foreach ($crawler as $domElement) { $countries = new Countries(); $countries->setCountry($domElement->getElementsByTagName('h2')->item(0)->textContent); $countries->setHotels($domElement->getElementsByTagName('span')->item(0)->textContent); $em = $this->getContainer()->get('doctrine'); $em->persist($countries); $em->flush(); } } 

    1 answer 1

    By its error message, PHP explicitly lets you know that an object of the Doctrine\Bundle\DoctrineBundle\Registry class does not have a persist method. And he, characteristically, is right.

    And the solution is trivial, you need to get an instance of EntityManager from the doctrine service:

     // Обратите внимание на "getManager". $em = $this->getContainer()->get('doctrine')->getManager(); $em->persist($countries); $em->flush(); 

    And here is a link to the official guide on the desired topic.

    • Thanks, I did a little differently: $ countries-> setCountry ($ domElement-> getElementsByTagName ('h2') -> item (0) -> textContent); $ countries-> setHotels ($ domElement-> getElementsByTagName ('span') -> item (0) -> textContent); $ doctrine = $ this-> getContainer () -> get ('doctrine'); $ em = $ doctrine-> getManager (); $ em-> persist ($ countries); $ em-> flush (); - Maxim Yakubenko
    • @MaximYakubenko, also not bad. The main thing is that you understood that EntityManager still needs to be extracted from the doctrine service :) - Dmitriy Simushev