Doctrine2 / Symfony Cheat Sheet

Column Types

Date

 

Query Builder

count

  • $qb = $this->createQueryBuilder('standort')
        ->select('count(standort.id)')
        ->where('standort.town=:town')
        ->setParameter('town', $town)
    ;
    
    $count = $qb->getQuery()->getSingleScalarResult();

andWhereIn

  • $qb->andWhere('user.name IN (:names)');
    $qb->setParameter('names', ['Karen', 'Kevin', 'Kim']);

offset / limit

  • $qb->setMaxResults($limit)
    $qb->setFirstResult($offset)
    

Use another repository in a repository

  • // in UserRepository.php
    $locations = $this->getEntityManager()->getRepository(Location::class) // without ...Repository!
        ->findDistinctLocations()

Migrations

Use PDO

  • $pdo = $this->connection->getNativeConnection();

Caching

$query->setResultCacheId('my_custom_id');
// or shorter notation with lifetime option
$query->useResultCache(true, 3600, 'my_custom_id');

// to delete cache
$cacheDriver = $entityManager->getConfiguration()->getResultCacheImpl();
$cacheDriver->delete('my_custom_id');
// to delete all cache entries
$cacheDriver->deleteAll();