Skip to content

How I can load the Product repository without using the dependency injection?

I made my own command to seed a table:

namespace MageGuideFirstModuleConsoleCommand;

use MageGuideFirstModuleModelBlogPost;
use MageGuideFirstModuleModelResourceModelBlogPostResource;

use MagentoFrameworkAppObjectManager;

use SymfonyComponentConsoleCommandCommand;

use SymfonyComponentConsoleHelperProgressBar;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;

/**
 * Seed Multiple Blogposts with Skus
 */
class BlogpostSeeder extends Command
{
    const BLOGPOSTS_NUM=10000;

    protected function configure(): void
    {
        $this->setName('db:seed:blogposts')->setDescription('Seed Multiple Blogposts');
        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $progressBar = new ProgressBar($output, self::BLOGPOSTS_NUM);

        // select random skus
  
        for($i=self::BLOGPOSTS_NUM;$i>0;$i--){
            $blogPost = ObjectManager::getInstance()->create(BlogPost::class);
            $blogPost->setTitle("BLogpost ".microtime());
            $blogPost->setPost("BLogpost Body".microtime());
            $resource = $blogPost->getResource();
            $resource->save($blogPost);
            $progressBar->advance();
        }


        $output->writeln("Success");
        return 0;
    }
}

And I want to select random skus and populate it into my Blogpost eg in the setPost(). A way to do it is to use the MagentoCatalogModelProductRepository class but I want to avoid using the dependency injection.

IS there a way to do this?