public function count($query = null)
{ /** If previous fetching exist - returns it. */
if (isset($this->cachingResult[__METHOD__])) {
return $this->cachingResult[__METHOD__];
}; /** Creates a new QueryBuilder instance that is prepopulated for this entity name. */
$createQueryBuilder = $this->getEntityManager()->createQueryBuilder();
if (!is_null($query)) {
$filterTagName = $query->getFilterTagName()["_0"];
} else {
$filterTagName = null;
}; /** Create query. */
$createQueryBuilder
->select ('COUNT(DISTINCT tag.id_tag) AS tagCount')
->from ($this->getClassName(), 'tag')
->where ('tag.name LIKE :filterTagName')
->setParameters (['filterTagName' => "%$filterTagName%"])
;
$this->cachingResult[__METHOD__] = $createQueryBuilder->getQuery()->getSingleScalarResult();
return $this->cachingResult[__METHOD__];
}
return [
/** Zend Framework component configurations. */
'service_manager' => [
'delegators' => [
'zfcuser_user_service' => [\Fork\ZfcUserDoctrineORM\Service\Delegator\AddDefaultRoleForRegisteredUserDelegator::class],
],
]
];
<?php
/**
* Zend Framework Application.
*
* @since 11:21 20.09.2017
*/
namespace Fork\ZfcUserDoctrineORM\Service\Delegator;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
/**
* AddDefaultRoleForRegisteredUserDelegator delegator.
*
* Configure instance of \ZfcUser\Service\User.
*
* @package Fork\ZfcUserDoctrineORM\Service\Delegator
*/
class AddDefaultRoleForRegisteredUserDelegator implements DelegatorFactoryInterface
{
/**
* {@inheritDoc}
*
* @link https://github.com/ZF-Commons/ZfcUser/wiki/How-to-store-custom-form-values-into-your-user-entity-at-registration
* @return \ZfcUser\Service\User
*/
public function __invoke(ContainerInterface $container, $requestedName, callable $callback, array $options = null)
{ /** Retrieve services. */
$zfcUserServiceUser = call_user_func($callback);
$entityManager = $container->get('Doctrine\ORM\EntityManager');
$zfcUserServiceUserEventManager = $zfcUserServiceUser->getEventManager();
/** Attach event for add default role. */
$zfcUserServiceUserEventManager->attach('register', function($mvcEvent) use ($entityManager) {
$user = $mvcEvent->getParam('user');
/** Fetched 'user' role as in DB. */
$role = $entityManager->getReference('Database\Entity\Role', 2);
/** Set for user the role user. */
$user->addRole($role);
$mvcEvent->setParam('user', $user);
});
return $zfcUserServiceUser;
}
}
$zfcUserServiceUserEventManager = $zfcUserServiceUser->getEventManager();
$zfcUserServiceUserEventManager->attach('register', function($mvcEvent) use ($entityManager) {
$user = $mvcEvent->getParam('user');
/** Fetched 'user' role as in DB. */
$role = $entityManager->getReference('Database\Entity\Role', 2);
/** Set for user the role user. */
$user->addRole($role);
$mvcEvent->setParam('user', $user);
});
Делайте отдельные запросы с LIMIT и OFFSET.