bka

private blog about development stuff

Magerun: no output on remote server

• Magento

Yesterday I had the case that on a remote machine n98-magerun was not working. It gave me no output at all. It seemed execution did not even start. Readme of magerun documents this issue like this:

On some Debian systems with compiled in suhosin the phar extension must be added to a whitelist.

Instead of adding this to php.ini, in my case it was sufficient to pass the whitelist as an argument. With this command I could use magerun as usual.

$ php -d suhosin.executor.include.whitelist="phar" shell/n98-magerun.phar dev:console

Error: Storefront properties changed

• Magento2

Error when creating custom widget

When you are creating a custom widget for Magento2 you may encounter following error:

Storefront Properties Changes have been made to this section that have not been saved. This tab contains invalid data. Please resolve this before saving.
Widget Options Changes have been made to this section that have not been saved. This tab contains invalid data. Please resolve this before saving.

I highly recomment checking your declared namespace. In may case the namespace did not match, after I moved the file, resulting in this speaking error above.

<?php

namespace Vendor\Modulename\Block\Adminhtml\Model\Widget;

Logging SQL queries

• Magento2

Logging SQL queries in Magento2

In order to enable logging of sql queries in Magento2 you need to edit app/etc/di.xml and change the type for LoggerInterface from Magento\Framework\DB\Logger\Quiet to Magento\Framework\DB\Logger\File.

<preference for="Magento\Framework\DB\LoggerInterface" type="Magento\Framework\DB\Logger\File"/>

Append this at the end of the file to pass required arguments:

<type name="Magento\Framework\DB\Logger\File">
    <arguments>
        <argument name="logAllQueries" xsi:type="boolean">true</argument>
        <argument name="debugFile" xsi:type="string">log/sql.log</argument>
    </arguments>
</type>

Because I’m paranoid, I also executed a bin/magento cache:clean afterwards and from now on all queries should be logged into var/log/sql.log.

Setting values into core_config_data with an install script

• Magento2

Setting values into core_config_data with an install script

It took me a while to figure out how to write configuration values within an install script. My first approach to use \Magento\Config\Model\Config was a dead end, because it requires some AuthenticationInterface and wants to initialize a session which does not play very well with command line tasks like bin/magento setup:upgrade. In the end it was the famous Area code not set Exception.

[Magento\Framework\Exception\LocalizedException]
Area code is not set

Instead of trying to use OOP and DI practice, just write them with a core db connection, this reduces a lot of pain. By the way, this is how other core modules are writing their values.

<?php

protected function saveConfigValue($path, $value){
  $data = [
      'scope' => 'default',
      'scope_id' => 0,
      'path' => $path,
      'value' => $value,
  ];
  $this->setup->getConnection()
      ->insertOnDuplicate($this->setup->getTable('core_config_data'), $data, ['value']);
}

XDebug for shell tasks

• Magento2

XDebug for shell tasks

Because I needed this some days ago: triggering XDEBUG when executing a shell command like bin/magento. This also works from inside a docker container on a vagrant machine. In the following example 192.168.56.1 actually is my host machine which serves as a gateway for the vagrant machine. For local debugging remote_host needs to be set localhost.

XDEBUG_CONFIG="idekey=vim" php -dxdebug.remote_host=192.168.56.1 -dxdebug.remote_enable=on -f bin/magento setup:upgrade

bin/magento: ServiceNotCreatedException

• Magento2

bin/magento ServiceNotCreatedException

I stumpeled upon a strange error message when running an upgrade task in magento2.

bin/magento setup:upgrade

magerun2 dev:console

• Magento2

magerun2 dev:console

Magento2 makes heavy use of Dependency Injection. There is no longer a need for the God-Class Mage to create objects like Mage::getModel("..."). The DI concept is a reasonable one and I like it but I was wondering how I could continue using magerun for testing code snippets. Use bin/n98-magerun2.phar dev:console in your shell which is a great tool for this purpose. However, it took me some time to figure out how to load Magento models without a Mage class. The solution is that the ObjectManager can be accessed directly to create required instances. Here is an example:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create("\Magento\Catalog\Model\ResourceModel\Product\Collection");
$product = $productCollection->getFirstItem();
$product->getData()