Installation:
composer require alexjumperman/doctrinetemptable
Ensure your composer.json includes doctrine/orm:^2.5 and doctrine/doctrine-bundle:^1.8.
Enable the Bundle (if using Symfony):
Add new AlexJumperman\TempTableBundle\AlexJumpermanTempTableBundle() to config/bundles.php.
First Use Case: Create a repository trait or service factory integration in a controller to filter a large dataset (e.g., 1M products) into a temporary table for efficient querying.
Extend Base Repository:
use AlexJumperman\TempTableBundle\Utils\TempTableTrait;
class ProductRepository extends \Doctrine\ORM\EntityRepository
{
use TempTableTrait;
}
createQueryBuilderForTempTable($alias) to generate a query builder for temporary tables.Controller Workflow:
$qb = $this->getDoctrine()
->getRepository(Product::class)
->createQueryBuilderForTempTable('p')
->where('p.category_id = :category')
->setParameter('category', 1);
Create Temp Table Repository:
$tempRepo = $qb->createTempTableRepository('temp_products');
$tempRepo for lightweight queries (e.g., counts, filtered results) without hitting the main table.Inject Factory:
use AlexJumperman\TempTableBundle\Factory\TempTableFactory;
public function __construct(TempTableFactory $tempTableFactory) {
$this->tempTableFactory = $tempTableFactory;
}
Usage:
$qb = $this->tempTableFactory->createQueryBuilderForTempTable(
$this->getDoctrine()->getRepository(Product::class),
'p'
);
WHERE clauses in the temp table query builder to reduce dataset size before materializing the temp table.setMaxResults() and pagination on the temp table for large result sets.COUNT, SUM, or GROUP BY operations to avoid expensive joins on the main table.Connection Handling:
#temp in MySQL, temp schema in PostgreSQL). Ensure your DBMS supports them.$tempRepo->getEntityManager()->getConnection()->fetchAll('SHOW TABLES') (MySQL) to verify temp tables exist.Transaction Scope:
$tempRepo->getEntityManager()->getConnection()->executeStatement('DROP TABLE IF EXISTS temp_products');
Performance Caveats:
$qb->createTempTableRepository('temp_products')->addIndex('idx_price', 'price');
Query Inspection:
getSQL() on the temp table query builder to verify generated SQL:
$sql = $qb->select('p.id')->getSQL();
Logging:
# config/packages/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
Configuration Quirks:
config/bundles.php. For manual setups, register it in AppKernel.php.Custom Temp Table Naming:
TempTableTrait and implementing getTempTableName():
protected function getTempTableName(): string
{
return 'custom_temp_' . uniqid();
}
Schema Customization:
TempTableRepository to add custom columns or constraints:
$tempRepo = $qb->createTempTableRepository('temp_products', [
'additional_column' => 'VARCHAR(255)'
]);
Integration with Caching:
$cacheKey = 'temp_products_' . md5($qb->getSQL());
$cachedResults = $this->cache->get($cacheKey, function() use ($tempRepo) {
return $tempRepo->findAll();
});
How can I help you explore Laravel packages today?