Question

Yes I know, another "Type Error occurred when creating object" post... In addition, I'm probably going about this in a way that will make the seasoned M2 dev need a moment before reading through this post so if I should just forget the error and reimplement what I'm doing in a better way, let me know.

The full error is as follows:

1 exception(s):
Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: VendorName\CustomModel\Model\Item

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: VendorName\CustomModel\Model\Item
#1 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() called at [vendor/magento/framework/ObjectManager/ObjectManager.php:56]
#2 Magento\Framework\ObjectManager\ObjectManager->create() called at [pub/misc/test.php:13]
#3 TestItems->launch() called at [generated/code/TestItems/Interceptor.php:24]
#4 TestItems\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
#5 Magento\Framework\App\Bootstrap->run() called at [pub/misc/test.php:20]

I've attempted all the usual fixes:

bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:clean

rm -rf /generated/*

But nothing I could find in previous times this has been asked solves my issue. I'm attempting to create an admin script that can be run by visiting a PHP file such as /misc/test.php, and am using the code at How can I bootstrap Magento 2 in a test.php script? to do this in the best way I can.

Below are my files:

/pub/misc/test.php

require __DIR__ . '/../../../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */

class TestItems
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        //dirty code goes here. 
        //the example below just prints a class name
        echo get_class($this->_objectManager->create('\VendorName\CustomModule\Model\Item'));
        //the method must end with this line
        return $this->_response;
    }
}

$app = $bootstrap->createApplication('TestItems');
$bootstrap->run($app);

/app/code/VendorName/CustomModule/Item.php

namespace VendorName\CustomModule\Model;

class Item extends \Magento\Framework\Model\AbstractModel
{
    protected $sourceItemsBySku;
    
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\InventoryApi\Api\GetSourceItemsBySkuInterface $sourceItemsBySku
    )
    {   
        $this->sourceItemsBySku = $sourceItemsBySku;
        parent::__construct($context);
    }
    
    public function getSourceItemBySku($sku)
    {
       return $this->sourceItemsBySku->execute($sku);
    }
}
Was it helpful?

Solution

Oddly enough this was caused by a failure during the setup:di:compile step.

Since I had errors not being reported in PHP I had no idea it was failing and I hadn't actually ever seen it compile successfully so I didn't know it wasn't supposed to stop after step 3, I just assumed the other steps went too quickly for it to report.

Add error reporting in bootstrap.php if you are also experiencing compile errors or the compiler stopping after one of the steps.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top