PHP code example of avro / csv-bundle
1. Go to this page and download the library: Download avro/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/ */
avro / csv-bundle example snippets
Avro\CsvBundle\AvroCsvBundle::class => ['all' => true],
Avro\CaseBundle\AvroCaseBundle::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 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\Export\ExporterInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class ExportController extends AbstractController
{
/**
* Export a db table.
*
* @param ExporterInterface $exporter The exporter
* @param string $alias The objects alias
*
* @return Response
*/
public function exportAction(ExporterInterface $exporter, string $alias): Response
{
$class = $this->getParameter(sprintf('avro_csv.objects.%s.class', $alias));
$exporter->init($class);
// customize the query
$qb = $exporter->getQueryBuilder();
$qb->where('o.fieldName =? 1')->setParameter(1, false);
$content = $exporter->getContent();
$response = new Response($content);
$response->headers->set('Content-Type', 'application/csv');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s.csv"', $alias));
return $response;
}
}