Page 3 - Monthly Archives: 2018
- 1 min read
If you receive the error message "No such entity.", "No such entity with" or "No such entity with customerId" in Magento 2, the issue usually occurred when you try to load not existing object via Magento 2 Repository Class.
To debug this , please open the following file:
vendor/magento/framework/Exception/NoSuchEntityException.php
and at the beginning of the __construct method temporary add debug backtrace code:
foreach (debug_backtrace() as $_stack) { echo ($_stack["file"] ? $_stack["file"] : '') . ':' . ($_stack["line"] ? $_stack["line"] : '') . ' - ' . ($_stack["function"] ? $_stack["function"] : '');}exit();
example:
public function __construct(Phrase $phrase = null, \Exception $cause = null, $code = 0) {
foreach (debug_backtrace() as $_stack) { echo ($_stack["file"] ? $_stack["file"] : '') . ':' . ($_stack["line"] ? $_stack["line"] : '') . ' - ' . ($_stack["function"] ? $_stack["function"] : ''); } exit();ihor
- 2 min read
Have you ever wondered how easy it is to check the quality of your own code, your colleagues' code, or a third-party module you want to use on a Magento project?
The Magento development team has created the Magento Extension Quality Program Coding Standard (Magento EQP), which allows you to check the code compliance with the standard, as well as identify the following shortcomings:
- SQL queries execution in the loop;- use of dangerous functions;- use of super-global variables;- excessive code complexity;- Unjustified collections loads.
Installation
You can install the Magento EQP via Composer, running this CLI command:
git clone https://github.com/magento/magento-coding-standard.gitcd magento-coding-standardcomposer install
In case you don't have the composer, you can download it here.
Checking
To check the code (which is, for example, in the /path/to/your/extension folder), go to the Magento EQP folder:
cd /path/to/magento-coding-standard
and run the followingihor
- 1 min read
When you update Magento 2, or switching git branches on your dev environment, sometimes you can get the error:
The following modules are outdated: Vendor_Module schema: current version - x.x.x, required version - z.z.z
To solve this issue, please do next:
1. Try to get the latest extension code. Run CLI command in the Magento root directory:
composer install
If you get troubles with running this command, then just skip it, and move to step 2.
2. Try to upgrade your database. Run CLI command:
php bin/magento setup:upgrade
3. If step 1 and 2 didn't help, then navigate to your database and run this SQL Query (don't forget to change the text in bold):
UPDATE `table_prfix_setup_module` SET schema_version="z.z.z", data_version="z.z.z" WHERE module="Vendor_Module";
"table_prfix_" can be found in app/etc/env.php, note that it can be empty.
- 3 min read
Imagine you are cooking pasta and sit down to play some game. You listen with one ear to see if the "dish" does not boil away. In this case, you are an observer. When the pasta starts to boil away — the event "pasta_began_to_boil_away" is triggered, which causes you (the observer) to rush to the kitchen (perform an action, an algorithm).
In Magento 2, as in real life, there are observers and events that are implemented on the basis of the "Publish-subscribe pattern". We have already described , which allows you to extend and change the functionality of the store. Let's see how you can do this with the help of events and observers in Magento 2.
Events
Events are triggered by Magento 2 modules when an action has occurred or has to occur. Magento has many native events and also allows you to call your own event.
Use the dispatch method of the \Magento\Framework\Event\ Manager class to call an event.
For example:
namespace MyCompany\MyModule;class MyClass{
/** *ihor
- 2 min read
You often need to change the JavaScript code logic located in the .js file. The easiest way is to override the js file is by using a theme. You can learn how to override view files in the article about . This is a quick but not an elegant way.
To change one or more js-file methods, use the mixins available in RequireJS.
To extend this file:
app/code/VendorName/ModuleName/view/%area%/web/js/folder1/folder2/somefile.js
with the following code:
define( [ 'jquery', 'underscore', 'ko', 'uiComponent', 'uiRegistry', ], function ( $, _, ko, Component, registry, ) { 'use strict';
return Component.extend({ // ... method1: function() { /* some code */ }, method2: function() { /* some code */ } // ... });
});
%area% - the area where the file is extended, for example: frontend, adminhtml, base.
To override theihor
- 1 min read
Usually, you get the error bash permission denied when running some script/file that does not have execute permissions. It is one of the . All you need to do to fix it is to change file permissions and add executive one.
To fix the bash permission denied error follow these steps:
1. Open terminal (shell)
2. Navigate to the folder with the script
3. Run the CLI command to change file permission settings:
chmod +x path_to_file/file_name
For example, if you run a :
bin/magento ...
and get the error:
bash: bin/magento: Permission denied
You need to add an execute (x) permission to the bin/magento file.
For this, please run the CLI command:
chmod +x bin/magento
In case of Magento 2 you can also use the next command to avoid the issue (php before bin/magento):
php bin/magento ...
Another issue you might face when running bin/magento commands, like the following ones, is the website breakdown during deployment:
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magentoihor
- 1 min read
All PHP programmers are familiar with the superglobal variables such as $_GET, $_REQUEST, $_POST, but their direct use is not allowed according to Magento2 code standards. So don't use them in your Magento 2 projects, especially if you want to pass the code Technical Review on the Magento Marketplace.
So what methods should be used?
To get data from the query, use the following methods: getParam($key, $default = null);getParams();getPostParam($key, $default = null);getPost();of the \Magento\Framework\App\RequestInterface class, for example:
protected $request;public function __construct( \Magento\Framework\App\RequestInterface $request, ....//інші параметри вашого класу) { $this->request = $request; ...//інший код конструктора}public function example() { // $data = $_REQUEST; $data = $this->request->getParams(); // $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null; $id = $this->request->getParam('id'); // $name = isset($_REQUEST['name'])ihor
- 1 min read
In Magento 2, one of the new properties is Magento 2 Virtual Type. What are they for?
Imagine that you already have a class A available:
namespace VendorName\ModuleName\Model
class A{ protected $arg1; protected $arg2; protected $arg3; public function __construct( Argument1 $arg1, Argument2 $arg2, Argument3 $arg3 ) { $this->arg1 = $arg1; $this->arg2 = $arg2; $this->arg3 = $arg3; }
}
And you need to create a class B, which will be inherited from A. However, it must take another argument as $arg2. To do this, you will create a new PHP file for class B:
namespace Me\MyModule\Model
class B extends A{ public function __construct( Argument1 $arg1, SomeOtherArg $someOtherArg, Argument2 $arg3 ) { parent::__construct($arg1, $someOtherArg, $arg3) }
}
But you don't need to do this in Magento 2. In such cases, virtual types are created. Therefore, create the etc/di.xmlihor
