Implement recipes

This commit is contained in:
Tim 2021-12-31 00:35:19 +01:00
parent f2a85c6c7d
commit d09cf430e1
8 changed files with 251 additions and 0 deletions

View File

@ -4,6 +4,8 @@ namespace App\Controller\Admin;
use App\Entity\Product; use App\Entity\Product;
use App\Entity\QuantityUnit; use App\Entity\QuantityUnit;
use App\Entity\Recipe;
use App\Entity\RecipeProductLine;
use App\Entity\User; use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard; use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem; use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
@ -39,6 +41,10 @@ class DashboardController extends AbstractDashboardController
yield MenuItem::linkToCrud('Products', 'fas fa-list', Product::class); yield MenuItem::linkToCrud('Products', 'fas fa-list', Product::class);
yield MenuItem::linkToCrud('Quantity units', 'fas fa-list', QuantityUnit::class); yield MenuItem::linkToCrud('Quantity units', 'fas fa-list', QuantityUnit::class);
yield MenuItem::section('Product lists', 'fas fa-folder-open');
yield MenuItem::linkToCrud('Recipes', 'fas fa-list', Recipe::class);
yield MenuItem::linkToCrud('Recipe product lines', 'fas fa-list', RecipeProductLine::class);
yield MenuItem::section('Administration', 'fas fa-folder-open'); yield MenuItem::section('Administration', 'fas fa-folder-open');
yield MenuItem::linkToCrud('User', 'fas fa-list', User::class); yield MenuItem::linkToCrud('User', 'fas fa-list', User::class);
} }

View File

@ -0,0 +1,38 @@
<?php
namespace App\Controller\Admin;
use App\Entity\Recipe;
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\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
class RecipeCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Recipe::class;
}
public function configureFields(string $pageName): iterable
{
$fields = [];
switch ($pageName) {
case Crud::PAGE_INDEX:
case Crud::PAGE_DETAIL:
$fields[] = IdField::new('id');
}
$fields[] = TextField::new('name');
$fields[] = TextField::new('description');
$fields[] = AssociationField::new('productLines');
return $fields;
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Controller\Admin;
use App\Entity\RecipeProductLine;
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\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
class RecipeProductLineCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return RecipeProductLine::class;
}
public function configureFields(string $pageName): iterable
{
$fields = [];
switch ($pageName) {
case Crud::PAGE_INDEX:
case Crud::PAGE_DETAIL:
$fields[] = IdField::new('id');
}
$fields[] = IntegerField::new('count');
$fields[] = AssociationField::new('product');
$fields[] = AssociationField::new('recipe');
return $fields;
}
}

View File

@ -21,6 +21,11 @@ abstract class AbstractProductLine
#[ORM\ManyToOne(targetEntity: Product::class)] #[ORM\ManyToOne(targetEntity: Product::class)]
private $product; private $product;
public function __toString(): string
{
return sprintf('%dx %s', $this->count, $this->product);
}
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;

View File

@ -25,6 +25,11 @@ class Product
#[ORM\ManyToOne(targetEntity: QuantityUnit::class)] #[ORM\ManyToOne(targetEntity: QuantityUnit::class)]
private $quantityUnit; private $quantityUnit;
public function __toString(): string
{
return $this->name;
}
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;

95
src/Entity/Recipe.php Normal file
View File

@ -0,0 +1,95 @@
<?php
namespace App\Entity;
use App\Repository\RecipeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: RecipeRepository::class)]
class Recipe
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $name;
#[ORM\Column(type: 'text', nullable: true)]
private $description;
#[ORM\OneToMany(mappedBy: 'recipe', targetEntity: RecipeProductLine::class)]
private $productLines;
public function __construct()
{
$this->productLines = new ArrayCollection();
}
public function __toString(): string
{
return $this->name;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection|RecipeProductLine[]
*/
public function getProductLines(): Collection
{
return $this->productLines;
}
public function addProductLine(RecipeProductLine $productLine): self
{
if (!$this->productLines->contains($productLine)) {
$this->productLines[] = $productLine;
$productLine->setRecipe($this);
}
return $this;
}
public function removeProductLine(RecipeProductLine $productLine): self
{
if ($this->productLines->removeElement($productLine)) {
// set the owning side to null (unless already changed)
if ($productLine->getRecipe() === $this) {
$productLine->setRecipe(null);
}
}
return $this;
}
}

View File

@ -7,5 +7,18 @@ use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity] #[ORM\Entity]
class RecipeProductLine extends AbstractProductLine class RecipeProductLine extends AbstractProductLine
{ {
#[ORM\ManyToOne(targetEntity: Recipe::class, inversedBy: 'productLines')]
private $recipe;
public function getRecipe(): ?Recipe
{
return $this->recipe;
}
public function setRecipe(?Recipe $recipe): self
{
$this->recipe = $recipe;
return $this;
}
} }

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Recipe;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Recipe|null find($id, $lockMode = null, $lockVersion = null)
* @method Recipe|null findOneBy(array $criteria, array $orderBy = null)
* @method Recipe[] findAll()
* @method Recipe[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class RecipeRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Recipe::class);
}
// /**
// * @return Recipe[] Returns an array of Recipe objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->orderBy('r.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Recipe
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}