Drupal: Force MySQL to run in ANSI compatability mode

Submitted by Peter Majmesku on Tue, 11/21/2017 - 03:56

Since Drupal (8) is supporting various database management systems (DBMS) like MySQL and PostgreSQL, not all database queries are behaving like directly in the DBMS, let's say MySQL.

E.g. Drupal is only supporting "full group by" which can be annoying. Because this removes the ability to group result rows on one specific database column. You will get errors like this:

ServiceCompetence.idServiceCompetence' isn't in GROUP BYSELECT *, GROUP_CONCAT(DISTINCT Services_serviceKeyFROM ServiceCompetence  
  WHERE Authorities_idAuthorities 
4

Such a query won't work in Drupal:

SELECT FROM ServiceCompetence sc
        WHERE Authorities_idAuthorities 
$authorityId
        GROUP BY sc
.Services_serviceKey

But luckily you can force Drupal to ANSI compatibility mode, for supporting features like single group by. Just use this 1 line of code:

/**
  * @var Drupal\Core\Database\Database
  */
$database->query('SET SESSION sql_mode = "ANSI,TRADITIONAL"'); 

More details can be found in this issue posting on Drupal.org: https://www.drupal.org/project/drupal/issues/344575#comment-1146016

Tags

Mix Prophecy Mock Framework with PHPUnit Mock Builder to Create Mocks for Not Existing Classes and Magic Methods

Submitted by Peter Majmesku on Wed, 08/16/2017 - 12:20

Prophecy is great as long as you are creating mocks for classes or methods, which are existing in your code base. The benefit is, that the usage is a bit more convenient compared with the PHPUnit mock builder (e.g. method definition and parameter handling). Therefor Prophecy is integrated into PHPUnit by default.

If you want to create mocks for classes, which are not existing in your code base or if you want to mock magic methods, this will become a problem. You could create a real class, just to have the class/method. But this is not an nice solution. That's why mixing mocking functionality from the Prophecy mocking framework and the PHPUnit mocking framework makes sense.

Here I am not questioning, if it is good to create tests for classes, that do not belong to your code base. I am just showing a way to handle this.

The following example works with PHPUnit 4.8.35 and up. The Prophecy mocking framework is part of PHPUnit.

// Mock the object via Prophecy - generally more convenient than with PHPUnit itself
$entityManager $this->prophesize(EntityManager::class);

// Create the mock object by PHPUnit, for a class that does not exist.
$shippingMethodRepository $this->getMockBuilder('ShippingMethod')
    ->
disableOriginalClone()
    ->
setMethods(['findByName'])
    ->
getMock();

// Set the not existing method.
$shippingMethodRepository->method('findByName')
    ->
willReturn(['Spedition''Hermes']);

// Define that getRepository() can retrieve any arguments and get a "real" object to test with.
$entityManager->getRepository(Prophecy\Argument::any())
    ->
willReturn($shippingMethodRepository);

// Mock a few methods with Prophecy as you like
$entityManager->persist(Prophecy\Argument::any())->willReturn(null);
$entityManager->flush(Prophecy\Argument::any())->willReturn(null);

// Get the mocked object, which will fake behavior
$entityManager->reveal();

// Will return 'Hermes'
end($entityManager->getRepository('AppBundle:ShippingMethod')->findByName('Delivery')); 

MySQL: Quickly Select Data with Multiple Conditions from One Table

Submitted by Peter Majmesku on Fri, 08/04/2017 - 11:09

Selecting data from one table with multiple conditions with Subselects is slow. Joins are quicker. Here is an example:

select * from OrderMetaData
Join OrderMetaData as a on OrderMetaData.id = a.Order_id and a.name = 'store' and a.value = 128
Join OrderMetaData as b on OrderMetaData.id = b.Order_id and b.name = 'processing_status' and b.value = 4;

This query takes 0,44 seconds in a table with > 800.000 entries. The sub-select takes 17 seconds.

Tags

CSS: Place Content in the Middle of a Div-Element via Flexbox

Submitted by Peter Majmesku on Mon, 07/31/2017 - 15:03

There are many approaches and often they are tricky. The simplest approach is the following and it does not need any additional wrapper-elements. Just place the CSS inside the selector, which is containing your content. The content will be positioned in the horizontal and vertical middle of the container. Mind that the Flexbox CSS feature is fully supported since IE 11.

display: flex;
justify-content: center;
align-items: center;

More nice Flexbox solutions can be found here.

Tags

Dumping Object/Array Stacks in Symfony

Submitted by Peter Majmesku on Mon, 07/10/2017 - 13:54

I prefer using XDebug for debugging PHP code. But sometimes there's a huge nesting in the project, which makes the application very slow. Then XDebug can become pain. So there's an alternative for inspecting the contents of objects:

\Doctrine\Common\Util\Debug::dump($user);

It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.