1

I have following code in UpgradeData.php

protected $test;

public function __construct(
        \Vendor\ModuleName\Model\Test $test
    )
    {
        $this->test = $test;
    }

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
....
        $this->test->install();
....
}

I have following code in Test.php

Vendor\ModuleName\Model\Test.php

class Test
{
protected $testmodel;

public function __construct(
        \Vendor\ModuleName\Model\TestModel $testmodel
    )
    {
        $this->testmodel = $testmodel;
    }
}

public function install(){
....
$result = $this->testmodel->getCollection()->addFieldToFilter('testcol','abcd')->getFirstItem();
....
}

Now, when I run

php bin/magento setup:upgrade

I get following error

enter image description here

When I remove TestModel class from constructor and its usage from install function, the upgrade script runs successfully.

I even tried to use solution mentioned in below link but none of them worked

'Area code not set' issue in custom CLI commands in Magento 2

I'm trying to fetch as well as insert data into database.

Edit #1 :

I even tried to use \Vendor\ModuleName\Model\ResourceModel\TestModel\CollectionFactory instead of \Vendor\ModuleName\Model\TestModel in my Test.php and when I run following command

$result = $this->testmodel->create();

It doesn't give any error on setup:upgrade command, but as soon as I try to use addFieldToFilter it gives the same error

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.