cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3 - How to handle Preference Error - Type Error occurred when creating object

Magento 2.3 - How to handle Preference Error - Type Error occurred when creating object

I am using Magento 2.3.4 small bug in it (LINK) then Update the following code

 

https://github.com/magento/magento2/blob/db69693bd929bd8665015ff6d9b6a3845584aa0b/lib/internal/Magen...

 

For testing, if i apply code directly

to vendor/magento/framework/Search/Dynamic/Algorithm.php bug solved.

 

For avoiding conflicts i am trying to use Preference,

 

app/code/Gta/Solver/etc/di.xml

<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\Search\Dynamic\Algorithm" type="Gta\Solver\Search\Dynamic\Algorithm" />
</config>

 

app/code/Gta/Solver/etc/module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
        <module name="Gta_Solver" schema_version="0.0.1" setup_version="0.0.1"></module>
</config>

 

app/code/Gta/Solver/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Gta_Solver',    __DIR__);

 

app/code/Gta/Solver/Search/Dynamic/Algorithm.php

Code : https://paste.ofcode.org/eggKeUuvGeE3izxV6K9rUC

 

After creating preference i am getting error, i am using the same code what i used for the test.

 

Error : 1 exception(s): Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Magento\Framework\Search\Dynamic\Algorithm\Improved

 

NOTE : I did di compile and clear the cache still i am facing an error.

How to solve the error.

 

4 REPLIES 4

Re: Magento 2.3 - How to handle Preference Error - Type Error occurred when creating object

Hi @Aveeva ,

 

Try with below alternative solution

1. Create the preference for the Magento\Framework\Search\Dynamic\Algorithm\Improved class as well.

2. In the preference class extend the base class and in constructor instead of passing 

Magento\Framework\Search\Dynamic\Algorithm

Pass your class object 

"Gta\Solver\Search\Dynamic\Algorithm" 

3. Do di compile and clear cache.

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: Magento 2.3 - How to handle Preference Error - Type Error occurred when creating object

@Nishu Jindal  Thank you, solved by following changes,

 

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Gta\Solver\Search\Dynamic;

use Magento\Framework\Search\Dynamic\IntervalInterface;  // ---------- 

/**
 * Algorithm for layer value filter
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 * @api
 */
class Algorithm extends \Magento\Framework\Search\Dynamic\Algorithm  // ----------
{

Re: Magento 2.3 - How to handle Preference Error - Type Error occurred when creating object

@Nishu Jindal  I hope my chances are good, is it right? 

Re: Magento 2.3 - How to handle Preference Error - Type Error occurred when creating object

Hi @Aveeva ,

 

Yes your changes are right. In preference we should extend base class for future updates safety.

 

Thanks!