Implement a query container

Edit on GitHub
When to use query containers

Don’t use query containers to cross module boundaries, as this increases module coupling. However, you can use them behind Repository and Entity Manager as query aggregations. Previously, query containers were used to cross-module borders (using dependency providers), which led to higher module coupling and leaking of the 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.

Create a new query container

To create a new query container, copy and paste the following snippet and replace MyBundle with your module name:

namespace Pyz\Zed\MyBundle\Persistence;

use Spryker\Zed\Kernel\Persistence\AbstractQueryContainer;

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

Conventions for query containers

These are conventions to follow:

  • All methods have the prefix query*().
  • All public methods are exposed in the related interface—for example, 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.