Skip to content

Instantly share code, notes, and snippets.

@loquace loquace/Orders.php
Last active Apr 20, 2018

Embed
What would you like to do?
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\ManyToMany;
use App\Entity\Products;
/**
* @ORM\Entity(repositoryClass="App\Repository\OrdersRepository")
*/
class Orders
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $customer;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Products", mappedBy="fkorders",cascade={"persist"})
*/
private $productsCollection;
public function __construct()
{
$this->productsCollection = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getCustomer(): ?string
{
return $this->customer;
}
public function setCustomer(string $customer): self
{
$this->customer = $customer;
return $this;
}
/**
* @return Collection|Products[]
*/
public function getProductsCollection(): Collection
{
return $this->productsCollection;
}
public function addProductsCollection(Products $productsCollection): self
{
if (!$this->productsCollection->contains($productsCollection)) {
$this->productsCollection[] = $productsCollection;
$productsCollection->setFkorders($this);
}
return $this;
}
public function removeProductsCollection(Products $productsCollection): self
{
if ($this->productsCollection->contains($productsCollection)) {
$this->productsCollection->removeElement($productsCollection);
// set the owning side to null (unless already changed)
if ($productsCollection->getFkorders() === $this) {
$productsCollection->setFkorders(null);
}
}
return $this;
}
}
<?php
namespace App\Controller;
use App\Entity\Orders;
use App\Form\OrdersType;
use App\Repository\OrdersRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Form\ProductsListType;
use App\Form\ProductsType;
use App\Entity\Products;
use App\Repository\ProductsRepository;
use App\Form\OrdersCompleteType;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Route("/orders")
*/
class OrdersController extends Controller {
/**
* @Route("/", name="orders_index", methods="GET")
*/
public function index(OrdersRepository $ordersRepository): Response {
return $this->render('orders/index.html.twig', ['orders' => $ordersRepository->findAll()]);
}
/**
* @Route("/new", name="orders_new", methods="GET|POST")
*/
public function new(Request $request): Response
{
$order = new Orders();
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
return $this->redirectToRoute('orders_show', ['id' => $order->getId()]);
}
return $this->render('orders/new.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="orders_show", methods="GET")
*/
public function show(Request $request, Orders $order): Response {
$products = new Products();
//echo $order->getId();
$form = $this->createForm(ProductsListType::class, $products, array('action' => $this->generateUrl('order_product', array('id' => $order->getId()))));
$form->handleRequest($request);
return $this->render('orders/show.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}/product", name="order_product", methods="POST")
*/
public function addProducts(Request $request, Orders $orders): Response {
$product = new Products();
$form = $this->createForm(ProductsListType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//Orders::$productsCollection = $productList;
$orders->addProductsCollection($product);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
}
return $this->redirectToRoute('orders_show', array('id' => $orders->getId()));
}
/**
* @Route("/{id}/edit", name="orders_edit", methods="GET|POST")
*/
public function edit(Request $request, Orders $order): Response {
$form = $this->createForm(OrdersType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('orders_edit', ['id' => $order->getId()]);
}
return $this->render('orders/edit.html.twig', [
'order' => $order,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="orders_delete", methods="DELETE")
*/
public function delete(Request $request, Orders $order): Response {
if ($this->isCsrfTokenValid('delete' . $order->getId(), $request->request->get('_token'))) {
$em = $this->getDoctrine()->getManager();
$em->remove($order);
$em->flush();
}
return $this->redirectToRoute('orders_index');
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\ManyToMany;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductsRepository")
*/
class Products
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $designation;
/**
* @ORM\Column(type="float")
*/
private $price;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Orders", inversedBy="productsCollection",cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $fkorders;
public function getId()
{
return $this->id;
}
public function getDesignation(): ?string
{
return $this->designation;
}
public function setDesignation(string $designation): self
{
$this->designation = $designation;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getFkorders(): ?Orders
{
return $this->fkorders;
}
public function setFkorders(?Orders $fkorders): self
{
$this->fkorders = $fkorders;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.