PHP code example of misatotremor / csv-bundle
1. Go to this page and download the library: Download misatotremor/csv-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/ */
misatotremor / csv-bundle example snippets
// config/bundles.php
return [
// ...
Avro\CaseBundle\AvroCaseBundle::class => ['all' => true],
Avro\CsvBundle\AvroCsvBundle::class => ['all' => true],
// ...
];
namespace Avro\CrmBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Avro\CsvBundle\Annotation\ImportExclude;
/**
* Avro\CrmBundle\Entity\Client
*
* @ORM\Entity
*/
class Client
{
/**
* @var string
*
* @ORM\Column(type="string", length=100, nullable=true)
* @ImportExclude
*/
protected $password;
// ...
}
namespace Avro\CrmBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Avro\CsvBundle\Annotation\ImportExclude;
#[ORM\Entity]
class Client
{
#[ORM\Column(type: 'string', length: 100, nullable: true)]
#[ImportExclude]
protected string $password;
// ...
}
namespace App\EventListener;
use Avro\CsvBundle\Event\AssociationFieldEvent;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Csv import listener
*/
class ImportListener implements EventSubscriberInterface
{
private $em;
/**
* @param EntityManagerInterface $em The entity manager
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public static function getSubscribedEvents()
{
return [
AssociationFieldEvent::class => 'importAssociation',
];
}
/**
* Set the objects createdBy field
*
* @param AssociationFieldEvent $event
*/
public function importAssociation(AssociationFieldEvent $event)
{
$association = $event->getAssociationMapping();
switch ($association['type']) {
case ClassMetadataInfo::ONE_TO_ONE:
case ClassMetadataInfo::MANY_TO_ONE:
$relation = $this->em->getRepository($association['targetEntity'])->findOneBy(
[
'name' => $event->getRow()[$event->getIndex()],
]
);
if ($relation) {
$event->getObject()->{'set'.ucfirst($association['fieldName'])}($relation);
}
break;
}
}
}
namespace App\EventListener;
use Avro\CsvBundle\Event\RowAddedEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
/**
* Csv import listener
*/
class ImportListener implements EventSubscriberInterface
{
private $em;
private $context;
/**
* @param EntityManagerInterface $em The entity manager
* @param SecurityContextInterface $context The security context
*/
public function __construct(EntityManagerInterface $em, SecurityContextInterface $context)
{
$this->em = $em;
$this->context = $context;
}
public static function getSubscribedEvents()
{
return [
RowAddedEvent::class => 'setCreatedBy',
];
}
/**
* Set the objects createdBy field
*
* @param RowAddedEvent $event
*/
public function setCreatedBy(RowAddedEvent $event)
{
$object = $event->getObject();
$user = $this->context->getToken()->getUser();
$object->setCreatedBy($user);
}
}
namespace App\Controller;
use Avro\CsvBundle\Event\ExportedEvent;
use Avro\CsvBundle\Event\ExportEvent;
use Avro\CsvBundle\Export\ExporterInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
class ExportController
{
private ExporterInterface $exporter;
private EventDispatcherInterface $eventDispatcher;
/**
* @psalm-var list<array{class: class-string, redirect_route: string}>
*/
private array $aliases;
/**
* ExportController constructor.
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
ExporterInterface $exporter,
array $aliases
) {
$this->eventDispatcher = $eventDispatcher;
$this->exporter = $exporter;
$this->aliases = $aliases;
}
/**
* Export a db table.
*
* @param string $alias The objects alias
*
* @return Response
*/
public function exportAction(string $alias): Response
{
$exporter->init($this->aliases[$alias]['class']);
$this->eventDispatcher->dispatch(new ExportEvent($this->exporter));
// customize the query
$qb = $exporter->getQueryBuilder();
$qb->where('o.fieldName =? 1')->setParameter(1, false);
$exportedEvent = new ExportedEvent($this->exporter->getContent());
$this->eventDispatcher->dispatch($exportedEvent);
$response = new Response($exportedEvent->getContent());
$response->headers->set('Content-Type', 'application/csv');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s.csv"', $alias));
return $response;
}
}