Skip to content
Advertisement

Symfony 6 – How to access properties of object of specific ID

I have two entities – one is Product, second – Order. I’ve created controllers to navigate between page of ProductController that lists the whole ProductRepository to the page of OrderController, which should:

  • take a Product ID to show its properties on the Order page (name and price)
  • have a form with quantity and “customer’s” email to be added to the Order
  • calculate the price x quantity and add to the Order

I can’t figure how to access the passed project’s properties. I am able to access it via addProduct method of the Order entity, but cannot reach them to show them in twig nor manipulate them on variables as described above.

I’m sure it’s a simple stuff but I am not sure even how to ask google the right way on this.

we click on a product from ProductRepository with

 <a href="{{ path('app_order', {id: product.id}) }}">

and we go to OrderController

OrderController:

<?php

namespace AppController;


class OrderController extends AbstractController
{
    #[Route('/order', name: 'app_order')]
    public function addToOrder(ManagerRegistry $doctrine, Request $request, EntityManagerInterface $em): Response
    {
        $orderedProductId = (int) ($request->query->get('id'));
        $pPrice = 1;
        $order = new Order();
        $order->addProduct($doctrine->getRepository(Product::class)->find($orderedProductId));
        $form = $this->createFormBuilder($order)
            ->add('clientEmail', TextType::class)
            ->add('quantity', IntegerType::class)
            ->add('makeOrder', SubmitType::class, ['label' => 'Make Order'])
            ->getForm();
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            //$data = $form->getData();
            $order->setStatus("Pending");
            $order->setTotalSum(1);
            $em->persist($order);
            $em->flush();
        }
        return $this->render('order/index.html.twig', [
            'form'=> $form->createView(),
            'order'=> $order
        ]);
    }
}

The entities look like this:

Product

<?php

namespace AppEntity;

use AppRepositoryProductRepository;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: ProductRepository::class)]
class Product
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private $id;

    #[ORMColumn(type: 'string')]
    private $name;

    #[ORMColumn(type: 'integer')]
    private $price;

    #[ORMColumn(type: 'boolean')]
    private $visible;

    #[ORMManyToOne(targetEntity: Order::class, inversedBy: 'product')]
    private $orderIds;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getPrice(): ?int
    {
        return $this->price;
    }

    public function setPrice(int $price): self
    {
        $this->price = $price;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function isVisible(): ?bool
    {
        return $this->visible;
    }

    public function setVisible(bool $visible): self
    {
        $this->visible = $visible;

        return $this;
    }

    public function getOrderIds(): ?Order
    {
        return $this->orderIds;
    }

    public function setOrderIds(?Order $orderIds): self
    {
        $this->orderIds = $orderIds;

        return $this;
    }
}

Order:

<?php

namespace AppEntity;

use AppRepositoryOrderRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: OrderRepository::class)]
#[ORMTable(name: '`order`')]
class Order
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private $id;

    #[ORMColumn(type: 'integer')]
    private $totalSum;

    #[ORMColumn(type: 'string', length: 255)]
    private $status;

    #[ORMColumn(type: 'integer')]
    private $quantity;

    #[ORMOneToMany(mappedBy: 'orderIds', targetEntity: Product::class)]
    private $product;

    #[ORMColumn(type: 'string', length: 255)]
    private $clientEmail;

    public function __construct()
    {
        $this->product = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTotalSum(): ?int
    {
        return $this->totalSum;
    }

    public function setTotalSum(int $totalSum): self
    {
        $this->totalSum = $totalSum;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(string $status): self
    {
        $this->status = $status;

        return $this;
    }

    public function getQuantity(): ?int
    {
        return $this->quantity;
    }

    public function setQuantity(int $quantity): self
    {
        $this->quantity = $quantity;

        return $this;
    }

    /**
     * @return Collection<int, Product>
     */
    public function getProduct(): Collection
    {
        return $this->product;
    }

    public function addProduct(Product $product): self
    {
        if (!$this->product->contains($product)) {
            $this->product[] = $product;
            $product->setOrderIds($this);
        }

        return $this;
    }

    public function removeProduct(Product $product): self
    {
        if ($this->product->removeElement($product)) {
            // set the owning side to null (unless already changed)
            if ($product->getOrderIds() === $this) {
                $product->setOrderIds(null);
            }
        }

        return $this;
    }

    public function getClientEmail(): ?string
    {
        return $this->clientEmail;
    }

    public function setClientEmail(string $clientEmail): self
    {
        $this->clientEmail = $clientEmail;

        return $this;
    }
}

Advertisement

Answer

OK I got this. Changed a property taken by OrderController from EntityManager to ProductRepository, then

$product= $productRepository->find($orderedProductId);

also: this is helpful: Getting only ID from entity relations without fetching whole object in Doctrine

solved!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement