2

i've written some upgrade-scripts for the database. When I'm running setup:upgrade from the CLI, I'm getting following error:

[Magento\Framework\Exception\SessionException]                       
Area code not set: Area code must be set before starting a session.  
[Magento\Framework\Exception\LocalizedException]  
Area code is not set

I tried the following areacode stuff in my UpgradeData-File with all possible area codes:

<?php
namespace ***\Catalog\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    private $_product;
    private $_productAction;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct( EavSetupFactory $eavSetupFactory, \Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product\Action $action, \Magento\Framework\App\State $appState, $name=null )
    {
        $appState->setAreaCode('admin');
        parent::__construct($name);
        $this->eavSetupFactory = $eavSetupFactory;
        $this->_product = $product;
        $this->_productAction = $action;
    }

    public function upgrade( ModuleDataSetupInterface $setup, ModuleContextInterface $context )
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create( [ 'setup' => $setup ] );
        $setup->startSetup();

        if( $context->getVersion() && version_compare( $context->getVersion(), '2.0.6' ) < 0 )
        {
            $eavSetup->updateAttribute( 4, 133, 'hide_price', 0 );
        }


        if( $context->getVersion() && version_compare( $context->getVersion(), '2.0.5' ) < 0 )
        {
            $attrData = array( 'hide_price' => 0, );

            $productIds = $this->_product->getCollection()->getAllIds();
            $this->_productAction->updateAttributes( $productIds, $attrData, 0 );
        }


        if( $context->getVersion() && version_compare( $context->getVersion(), '2.0.4' ) < 0 )
        {
            $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'hide_price', [ 'type' => 'varchar', 'backend' => '', 'frontend' => '', 'label' => 'Preis verstecken', 'input' => 'boolean', 'class' => '', 'source' => '', 'global' => \Magento\Store\Model\ScopeInterface::SCOPE_STORE, 'visible' => true, 'required' => false, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => '' ] );
        }

        $setup->endSetup();
    }
}

But nothing helps.

When I'm running the upgrade, the schema-version in the database gets updated but not the data_version.

Any thoughts?

Thanks in advance - Ali

  • Could you please post the full code of your class ? – Raphael at Digital Pianism Sep 8 '16 at 13:28
  • If I delete the following injected Objects \Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product\Action $action it works – user2244859 Sep 8 '16 at 13:29
  • Please edit your question with the full UpgradeData file code – Raphael at Digital Pianism Sep 8 '16 at 13:30
  • done //edited my question – user2244859 Sep 8 '16 at 13:36
  • Does it work if you replace $productIds = $this->_product->getCollection()->getAllIds(); with some hard coded ids like $productIds = [1,2,3];? The product collection probably tries to instantiate a customer session to get customer group prices, but I am not sure where the session instantiation for this happens in Magento 2. – Fabian Schmengler Sep 10 '16 at 12:01
1

I had this problem before and i solved it by try{} catch{}, try this code

<?php
namespace ***\Catalog\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    private $_product;
    private $_productAction;
    protected $state;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct( 
        EavSetupFactory $eavSetupFactory,
        \Magento\Catalog\Model\Product $product,
        \Magento\Catalog\Model\Product\Action $action, 
        \Magento\Framework\App\State $appState,
        $name=null 
    ) {

        $this->state = $appState;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->_product = $product;
        $this->_productAction = $action;
    }

    public function upgrade( ModuleDataSetupInterface $setup, ModuleContextInterface $context )
    {
        try{
            $this->state->getAreaCode();
        }
        catch (\Magento\Framework\Exception\LocalizedException $ex) {
            $this->state->setAreaCode('adminhtml');
        }

        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create( [ 'setup' => $setup ] );
        $setup->startSetup();

        if( $context->getVersion() && version_compare( $context->getVersion(), '2.0.6' ) < 0 )
        {
            $eavSetup->updateAttribute( 4, 133, 'hide_price', 0 );
        }


        if( $context->getVersion() && version_compare( $context->getVersion(), '2.0.5' ) < 0 )
        {
            $attrData = array( 'hide_price' => 0, );

            $productIds = $this->_product->getCollection()->getAllIds();
            $this->_productAction->updateAttributes( $productIds, $attrData, 0 );
        }


        if( $context->getVersion() && version_compare( $context->getVersion(), '2.0.4' ) < 0 )
        {
            $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'hide_price', [ 'type' => 'varchar', 'backend' => '', 'frontend' => '', 'label' => 'Preis verstecken', 'input' => 'boolean', 'class' => '', 'source' => '', 'global' => \Magento\Store\Model\ScopeInterface::SCOPE_STORE, 'visible' => true, 'required' => false, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => '' ] );
        }

        $setup->endSetup();
    }
}
0

Pretty sure your upgrade script is not properly coded.

First, I don't think you have to specify the area code in your constructor and as the class does not extend anything, you don't have to call the parent constructor.

Thus you can replace:

public function __construct( EavSetupFactory $eavSetupFactory, \Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product\Action $action, \Magento\Framework\App\State $appState, $name=null )
{
    $appState->setAreaCode('admin');
    parent::__construct($name);
    $this->eavSetupFactory = $eavSetupFactory;
    $this->_product = $product;
    $this->_productAction = $action;
}

With:

public function __construct( EavSetupFactory $eavSetupFactory, \Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product\Action $action)
{
    $this->eavSetupFactory = $eavSetupFactory;
    $this->_product = $product;
    $this->_productAction = $action;
}

On top of that you should start the setup before doing anything else so replace:

    $eavSetup = $this->eavSetupFactory->create( [ 'setup' => $setup ] );
    $setup->startSetup();

With:

    $setup->startSetup();
    $eavSetup = $this->eavSetupFactory->create( [ 'setup' => $setup ] );
  • Thank you for your reply. As I mentioned before, if I remove the product and action part, it works fine. – user2244859 Sep 9 '16 at 7:03

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.