Skip to content

unable to save a rewrite

Im trying to save a new redirects into the urlrewrite model but it seems that save() doesn’t exist in those factorys:

 MagentoUrlRewriteModelUrlRewriteFactory or
 MagentoUrlRewriteModelResourceModelUrlRewriteFactory or
 Magento/UrlRewrite/Service/V1/Data/UrlRewriteFactory (missing save method)

this is my code:

<?php

declare(strict_types=1);

namespace ItonomyCreateRedirectsConsoleCommand;
use MagentoUrlRewriteModelUrlRewrite as BaseUrlRewrite;
use MagentoUrlRewriteModelResourceModelUrlRewriteCollectionFactory;
use MagentoUrlRewriteServiceV1DataUrlRewriteFactory;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use PsrLogLoggerInterface;
use SymfonyComponentConsoleQuestionChoiceQuestion;
use MagentoStoreModelStoreManagerInterface;


class CreateRedirectsCommand extends Command
{
    /**
     * @var UrlRewriteCollectionFactory
     */
    private UrlRewriteCollectionFactory $urlRewriteCollectionFactory;

    /**
     * @var LoggerInterface
     */
    private LoggerInterface $logger;

    /**
     * @var StoreManagerInterface
     */
    private StoreManagerInterface $storeManager;

    /**
     * @var UrlRewriteFactory
     */
    private UrlRewriteFactory $urlRewriteFactory;

    /**
     * @param UrlRewriteCollectionFactory $urlRewriteCollectionFactory
     * @param LoggerInterface $logger
     * @param StoreManagerInterface $storeManager
     * @param UrlRewriteFactory $urlRewriteFactory
     */
    public function __construct(
        UrlRewriteCollectionFactory $urlRewriteCollectionFactory,
        LoggerInterface $logger,
        StoreManagerInterface $storeManager,
        UrlRewriteFactory $urlRewriteFactory
    ) {
        $this->urlRewriteCollectionFactory = $urlRewriteCollectionFactory;
        $this->logger = $logger;
        $this->storeManager = $storeManager;
        $this->urlRewriteFactory = $urlRewriteFactory;
        parent::__construct();
    }

    /**
     * @return void
     */
    protected function configure()
    {
        $this->setName('itonomy:generate-redirects')
            ->setDescription('Generates 301 redirects for entries in the url_rewrite table.');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return void
     */
    
    
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $storeManagerDataList = $this->storeManager->getStores();

        // retrieving all stores
        $storeViews = array();

        foreach ($storeManagerDataList as $key => $value) {
            $storeViews[$key] = $value['code'];
        }

        // displaying stores for user
        $question = new ChoiceQuestion(
            'Please select your Store_Id', $storeViews, 0
        );
        $question->setErrorMessage('Store %s is invalid.');

        $helper = $this->getHelper('question');
        $store = $helper->ask($input, $output, $question);
        $storeViewId = array_search($store, $storeViews);
        $output->writeln('You have just selected: ' . $storeViewId);

        // collection results pagination
        $pageSize = 100;
        $page = 1;

        $urlRewriteCollection = $this->urlRewriteCollectionFactory->create();
        $urlRewriteCollection->addFieldToFilter('store_id', $storeViewId);
        $urlRewriteCollection
            ->setPageSize($pageSize)
            ->setCurPage($page);

        $collectionSize = $urlRewriteCollection->getSize();

        //From DotdigitalgroupEmailModelAbandonedCartProgramEnrolmentEnroller::getStoreQuotesForGuestsAndCustomers
        for ($i = 0; $i < $collectionSize; $i += $pageSize) {

            $urlRewriteCollection->getSize();

            foreach ($urlRewriteCollection as $urlRewrite) {
                $requestPath = $urlRewrite->getRequestPath();
                if (substr($requestPath, -5) !== '.html') {
                    try {
                        
                        $newUrlRedirect = $this->urlRewriteFactory->create()->setStoreId($storeViewId)
                            ->setEntityType($urlRewrite->getEntityType())
                            ->setEntityId($urlRewrite->getEntityType())
                            ->setRequestPath($urlRewrite->getRequestType() . '.html')
                            ->setTargetPath($urlRewrite->getRequestType())
                            ->setIsAutogenerated(0)
                            ->setRedirectType(301);

                        $newUrlRedirect->save();
                    } catch (Exception $e) {
                        $this->logger->error($e->getMessage());
                    }
                }
            }

            $page++;
            $urlRewriteCollection
                ->setCurPage($page);
        }

        $output->writeln('Redirects generated successfully.');
    }

it gives an error like this:

Fatal error: Uncaught Error: Call to undefined method MagentoUrlRewriteServiceV1DataUrlRewrite::save()