Question

my Magento 2 runs in developer mode and I'm trying to install my first selfwritten extension by CLI. The install itself works nice, but when running the setup:upgrade command, it can't find the injected Factories in my UpgradeData.php scripts.

Here a condensed version of the code:

public function _construct(
    \Vendor\Extension\Model\DepartmentFactory $departmentFactory,
    \Vendor\Extension\Model\EmployeeFactory $employeeFactory
)
{
    $this->departmentFactory = $departmentFactory;
    $this->employeeFactory = $employeeFactory;
    ...
    $salesDepartment = $this->departmentFactory->create();
    ...
}


PHP Fatal error:  Uncaught Error: Call to a member function create() on null in /var/www/html/magento/app/code/Vendor/Extension/Setup/UpgradeData.php:30

Not even the Magento\Framework\Code\Generator\Autoloader is requested at this point. Many other classes are successfully requested an generated while running other setup:upgrade tasks on earlier modules, but here, the Autoloader is not called at all (checked that by echoing in the class).

As far as I understood, neither do I need to write this classes, nor do I have to write a preference for it. Please correct me if I'm wrong.

So what could the problem be? What do I have to change?

Many thanks.

Was it helpful?

Solution

The constructor is defined incorrectly, it should be:

public function __construct(..

With a double underscore for the autoloading / dependency injection to work.

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