Implementing a Query Container

Edit on GitHub
When to use Query Containers

Don’t use query containers to cross module boundaries, as this increases modules coupling. However, you can use them behind Repository and Entity Manager as query aggregations. Previously, query containers were used to cross module borders (via dependency providers), which led to higher module coupling and leaking of persistence layer from one domain object to another, and therefore, to higher maintenance efforts and lower code reusability. This approach has been deprecated now, so we don’t recommend using query containers like this in your project development.

To create a new Query Container you can copy and paste the following snippet and replace MyBundle with your module name.

<?php
namespace Pyz\Zed\MyBundle\Persistence;

use Spryker\Zed\Kernel\Persistence\AbstractQueryContainer;

/**
 * @method MyBundlePersistenceFactory getFactory()
 */
class MyBundleQueryContainer extends AbstractQueryContainer implements MyBundleQueryContainerInterface
{
}

Conventions for Query Containers

There are some conventions which should be followed here:

  • All methods have the prefix query*().
  • All public methods are exposed in the related interface (e.g. MyBundleQueryContainerInterface).
  • Queries are returned unterminated, so that the user can add restrictions (limit, offset) and can choose how to terminate (count(), find(), findAll()).
  • Query containers do not access higher layers. So no usage of a facade here.
  • Query containers do not contain any logic which is not needed to build queries.