0

I made a seeder custom command in order to populate multiple models for my developemtn easyness:

<?php

namespace MageGuide\FirstModule\Console\Command;

use MageGuide\FirstModule\Model\BlogPost;
use MageGuide\FirstModule\Model\ResourceModel\BlogPostResource;
use Symfony\Component\Console\Command\Command;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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

    private BlogPostResource $resource;

    public function __construct(BlogPostResource $resource){
        $this->resource=$resource;
        parent::__construct('db:seed:blogposts');
    }

    protected function configure(): void
    {
        $this->setDescription('Seed Multiple Blogposts in DB');
        parent::configure();
    }

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

        for($i=self::BLOGPOSTS_NUM;$i>0;$i--){
            $blogPost = new BlogPost();
            $blogPost->setTitle("BLogpost ".microtime());
            $blogPost->setBody("BLogpost Body".microtime());
            $this->resource->save($blogPost);
            $progressBar->advance();
        }
        return 0;
    }
}

But I need to populate extra stuff at BlogPost Model in my component. So how I can create a new model instance in my Command so I can populate my DB with blogposts?

2 Answers 2

0

Create a model factory for your BlogPost model. This should be located in your module and might look like this:

// app/code/MageGuide/FirstModule/Model/BlogPostFactory.php
<?php

namespace MageGuide\FirstModule\Model;

use Magento\Framework\ObjectManagerInterface;

class BlogPostFactory
{
    private $objectManager;

    public function __construct(ObjectManagerInterface $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function create()
    {
        return $this->objectManager->create(BlogPost::class);
    }
}

Modify your command to accept the BlogPostFactory via dependency injection:

// app/code/MageGuide/FirstModule/Console/Command/BlogpostSeeder.php

// ...
use MageGuide\FirstModule\Model\BlogPostFactory;
// ...

class BlogpostSeeder extends Command
{
    // ...
    private BlogPostFactory $blogPostFactory;

    public function __construct(BlogPostResource $resource, BlogPostFactory $blogPostFactory)
    {
        $this->resource = $resource;
        $this->blogPostFactory = $blogPostFactory;
        parent::__construct('db:seed:blogposts');
    }
    // ...

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

        for ($i = self::BLOGPOSTS_NUM; $i > 0; $i--) {
            $blogPost = $this->blogPostFactory->create();
            $blogPost->setTitle("Blogpost " . microtime());
            $blogPost->setBody("Blogpost Body" . microtime());
            // Set additional data here if needed
            $this->resource->save($blogPost);
            $progressBar->advance();
        }
        return 0;
    }
    // ...
}

With these changes, you can now create and populate instances of the BlogPost model using the factory, and you have the flexibility to set additional data as needed before saving them to the database.

0
  1. Do not use new where there is no direct need.
  2. Adobe Commerce has factories for creating new objects, here is the documentation, please read https://developer.adobe.com/commerce/php/development/components/factories/

I've updated your code a bit so it should work.

<?php

namespace MageGuide\FirstModule\Console\Command;

use MageGuide\FirstModule\Model\BlogPostFactory;
use MageGuide\FirstModule\Model\ResourceModel\BlogPostResource;
use Symfony\Component\Console\Command\Command;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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

    public function __construct(
        private BlogPostResource $resource,
        private BlogPostFactory $blogPostFactory)
    {
        parent::__construct('db:seed:blogposts');
    }

    protected function configure(): void
    {
        $this->setDescription('Seed Multiple Blogposts in DB');
        parent::configure();
    }

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

        for($i=self::BLOGPOSTS_NUM;$i>0;$i--){
            $blogPost = $this->blogPostFactory->cretae();
            $blogPost->setTitle("BLogpost ".microtime());
            $blogPost->setBody("BLogpost Body".microtime());
            $this->resource->save($blogPost);
            $progressBar->advance();
        }
        return 0;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.