0

I am using Magento 2.2.6. The file I am extending is \Magento\UrlRewrite\Model\Storage\DbStorage. It has the following constructor:

/**
 * DB Storage table name
 */
const TABLE_NAME = 'url_rewrite';

/**
 * Code of "Integrity constraint violation: 1062 Duplicate entry" error
 */
const ERROR_CODE_DUPLICATE_ENTRY = 1062;

/**
 * @var \Magento\Framework\DB\Adapter\AdapterInterface
 */
protected $connection;

/**
 * @var Resource
 */
protected $resource;

/**
 * @var \Psr\Log\LoggerInterface
 */
private $logger;

/**
 * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory
 * @param DataObjectHelper $dataObjectHelper
 * @param \Magento\Framework\App\ResourceConnection $resource
 * @param \Psr\Log\LoggerInterface|null $logger
 */
public function __construct(
    UrlRewriteFactory $urlRewriteFactory,
    DataObjectHelper $dataObjectHelper,
    ResourceConnection $resource,
    LoggerInterface $logger = null
) {
    $this->connection = $resource->getConnection();
    $this->resource = $resource;
    $this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()
        ->get(\Psr\Log\LoggerInterface::class);

    parent::__construct($urlRewriteFactory, $dataObjectHelper);
}

Here is what I have so far:

/**
 * @var \Psr\Log\LoggerInterface
 */
private $logger;

/**
 * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory
 * @param DataObjectHelper $dataObjectHelper
 * @param \Magento\Framework\App\ResourceConnection $resource
 * @param \Psr\Log\LoggerInterface|null $logger
 */
public function __construct(
    UrlRewriteFactory $urlRewriteFactory,
    DataObjectHelper $dataObjectHelper,
    ResourceConnection $resource,
    LoggerInterface $logger = null
) {
    $this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()
        ->get(\Psr\Log\LoggerInterface::class);

    parent::__construct($urlRewriteFactory, $dataObjectHelper, $resource);
}

Do I want to change:

parent::__construct($urlRewriteFactory, $dataObjectHelper, $resource);

to

parent::__construct($urlRewriteFactory, $dataObjectHelper, $resource, $logger);

?

0

If you are extending a class and overriding the __construct() function you have to pass the parameters that parent requires. So \Magento\UrlRewrite\Model\Storage\DbStorage::__construct has folowing params

public function __construct(
    UrlRewriteFactory $urlRewriteFactory,
    DataObjectHelper $dataObjectHelper,
    ResourceConnection $resource,
    LoggerInterface $logger = null
)

So the answer is that you need call parent like this parent::__construct($urlRewriteFactory, $dataObjectHelper, $resource, $logger);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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