PHP code example of tourze / async-export-bundle

1. Go to this page and download the library: Download tourze/async-export-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / async-export-bundle example snippets




use AsyncExportBundle\Entity\AsyncExportTask;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;

// Create a new export task
$task = new AsyncExportTask();
$task->setDql('SELECT u FROM App\Entity\User u WHERE u.active = true');
$task->setEntityClass('App\Entity\User');
$task->setColumns(['id', 'email', 'name', 'createTime']);
$task->setFile('users_export.csv');
$task->setRemark('Export active users');
$task->setUser($security->getUser()); // Associate with current user
$task->setValid(true);

// Save the task
$entityManager->persist($task);
$entityManager->flush();

// The task is now ready for background processing
// Use Symfony Messenger or similar for async execution



return [
    // ...
    AsyncExportBundle\AsyncExportBundle::class => ['all' => true],
];



use AsyncExportBundle\Entity\AsyncExportTask;
use AsyncExportBundle\Repository\AsyncExportTaskRepository;
use Doctrine\ORM\EntityManagerInterface;

// Get repository
$repository = $entityManager->getRepository(AsyncExportTask::class);

// Find tasks by user
$userTasks = $repository->findBy(['user' => $user]);

// Find valid/pending tasks
$validTasks = $repository->findBy(['valid' => true]);

// Find tasks by entity class
$entityTasks = $repository->findBy(['entityClass' => 'App\Entity\User']);

// Find tasks with progress tracking
$incompleteTasks = $repository->createQueryBuilder('t')
    ->where('t.processCount < t.totalCount')
    ->andWhere('t.valid = true')
    ->getQuery()
    ->getResult();

// Update task progress during processing
$task->setTotalCount(1000);
$task->setProcessCount(250); // 25% complete
$task->setMemoryUsage(memory_get_usage());
$entityManager->flush();

try {
    // Process export
    processExport($task);
} catch (\Exception $e) {
    $task->setException($e->getMessage());
    $task->setValid(false);
    $entityManager->flush();
}