Skip to content

Instantly share code, notes, and snippets.

@julienanquetil
Created February 25, 2021 12:24
Show Gist options
  • Save julienanquetil/4cee2a0f0780b43d897f6269a830db46 to your computer and use it in GitHub Desktop.
Save julienanquetil/4cee2a0f0780b43d897f6269a830db46 to your computer and use it in GitHub Desktop.
EasyAdmin Crud
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ORM\Table(name="medias")
* @Vich\Uploadable
*/
class Media
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @var int
*/
protected $id;
/**
* Many medias have one car. This is the owning side.
* @ORM\ManyToOne(targetEntity="Voiture", inversedBy="images")
* @ORM\JoinColumn(name="voiture_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
*/
protected $voiture;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName", size="imageSize")
*
* @var File|null
*/
private $imageFile;
/**
* @ORM\Column(type="string")
*
* @var string|null
*/
private $imageName;
/**
* @ORM\Column(type="integer")
*
* @var int|null
*/
private $imageSize;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTimeInterface|null
*/
private $updatedAt;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
*/
public function setImageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
if (null !== $imageFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getVoiture()
{
return $this->voiture;
}
/**
* @param mixed $voiture
*/
public function setVoiture($voiture): void
{
$this->voiture = $voiture;
}
/**
* @return \DateTimeInterface|null
*/
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
/**
* @param \DateTimeInterface|null $updatedAt
*/
public function setUpdatedAt(?\DateTimeInterface $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
}
<?php
namespace App\Controller\Admin;
use App\Entity\Voiture;
use App\Form\AttachmentType;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Vich\UploaderBundle\Form\Type\VichImageType;
class VoitureCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Voiture::class;
}
public function configureFields(string $pageName): iterable
{
$fields = [
TextField::new('externalID'),
TextField::new('externalURL'),
TextField::new('video'),
IntegerField::new('year'),
TextField::new('brand'),
TextField::new('model'),
TextareaField::new('description'),
IntegerField::new('price'),
TextField::new('engine'),
TextField::new('transmission'),
BooleanField::new('status'),
BooleanField::new('toscrape'),
BooleanField::new('featured'),
AssociationField::new('images'),
CollectionField::new('images')
->setEntryType(AttachmentType::class)
->setFormTypeOption('by_reference', false)
->onlyOnForms(),
CollectionField::new('images')
->setTemplatePath('admin/image.html.twig')
->onlyOnDetail(),
];
/*if ($pageName == Crud::PAGE_INDEX || $pageName == Crud::PAGE_DETAIL){
}*/
return $fields;
}
public function configureActions(Actions $actions): Actions
{
return $actions->add(Crud::PAGE_INDEX,'detail');
}
}
<?php
namespace App\Entity;
use App\Repository\VoitureRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass=VoitureRepository::class)
* @ORM\Table(name="voitures")
*/
class Voiture{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $externalID;
/**
* @ORM\Column(type="text")
*/
protected $externalURL;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $video;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $year;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $brand;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $model;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $description;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $price;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $engine;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $transmission;
/**
* @ORM\Column (type="boolean", options={"default":"0"})
*/
protected $status = false;
/**
* @ORM\Column (type="boolean", options={"default":"0"})
*/
protected $toscrape;
/**
* @ORM\OneToMany(targetEntity="Media", mappedBy="voiture", cascade={"persist", "remove"})
*/
protected $images;
/**
* @ORM\Column (type="boolean", options={"default":"0"})
*/
protected $featured;
/**
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @ORM\Column(type="string", length=100, unique=true)
* @Gedmo\Slug(fields={"brand","model","year","externalID"})
*/
private $slug;
public function __construct()
{
$this->created = new \DateTime();
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getExternalID()
{
return $this->externalID;
}
/**
* @param mixed $externalID
*/
public function setExternalID($externalID): void
{
$this->externalID = $externalID;
}
/**
* @return mixed
*/
public function getExternalURL()
{
return $this->externalURL;
}
/**
* @param mixed $externalURL
*/
public function setExternalURL($externalURL): void
{
$this->externalURL = $externalURL;
}
/**
* @return mixed
*/
public function getVideo()
{
return $this->video;
}
/**
* @param mixed $video
*/
public function setVideo($video): void
{
$this->video = $video;
}
/**
* @return mixed
*/
public function getYear()
{
return $this->year;
}
/**
* @param mixed $year
*/
public function setYear($year): void
{
$this->year = $year;
}
/**
* @return mixed
*/
public function getBrand()
{
return $this->brand;
}
/**
* @param mixed $brand
*/
public function setBrand($brand): void
{
$this->brand = $brand;
}
/**
* @return mixed
*/
public function getModel()
{
return $this->model;
}
/**
* @param mixed $model
*/
public function setModel($model): void
{
$this->model = $model;
}
/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}
/**
* @param mixed $description
*/
public function setDescription($description): void
{
$this->description = $description;
}
/**
* @return mixed
*/
public function getPrice()
{
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice($price): void
{
$this->price = $price;
}
/**
* @return mixed
*/
public function getEngine()
{
return $this->engine;
}
/**
* @param mixed $engine
*/
public function setEngine($engine): void
{
$this->engine = $engine;
}
/**
* @return mixed
*/
public function getTransmission()
{
return $this->transmission;
}
/**
* @param mixed $transmission
*/
public function setTransmission($transmission): void
{
$this->transmission = $transmission;
}
/**
* @return bool
*/
public function isStatus(): bool
{
return $this->status;
}
/**
* @param bool $status
*/
public function setStatus(bool $status): void
{
$this->status = $status;
}
/**
* @return mixed
*/
public function getToscrape()
{
return $this->toscrape;
}
/**
* @param mixed $toscrape
*/
public function setToscrape($toscrape): void
{
$this->toscrape = $toscrape;
}
/**
* Get status.
*
* @return bool
*/
public function getStatus()
{
return $this->status;
}
/**
* @return mixed
*/
public function getImages()
{
return $this->images;
}
/**
* @param mixed $images
*/
public function setImages($images): void
{
$this->images = $images;
}
/**
* @return mixed
*/
public function getFeatured()
{
return $this->featured;
}
/**
* @param mixed $featured
*/
public function setFeatured($featured): void
{
$this->featured = $featured;
}
/**
* @return mixed
*/
public function getCreated()
{
return $this->created;
}
/**
* @param mixed $created
*/
public function setCreated($created): void
{
$this->created = $created;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment