我有两个实体-一个是Product,第二个是Order。我已经创建了用于在列出整个productrerepository的ProductController页面和OrderController页面之间导航的控制器,它应该:
- 接受产品ID,以在订单页面上显示其属性(名称和价格)
- 有数量和"客户"的形式;要添加到订单中的电子邮件
- 计算价格x数量并添加到订单
我不知道如何访问通过的项目的属性。我可以通过Order实体的addProduct方法访问它,但是不能像上面描述的那样在小枝中显示它们,也不能在变量上操作它们。
我相信这是一个简单的东西,但我不确定如何询问谷歌正确的方法。
我们用
从productrerepository点击一个产品<a href="{{ path('app_order', {id: product.id}) }}">
然后进入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
]);
}
}
实体如下所示:
产品
<?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;
}
}
:
<?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;
}
}
好的,我知道了。将OrderController获取的属性从EntityManager更改为ProductRepository,然后
$product= $productRepository->find($orderedProductId);
也:这是有用的:从实体关系中获取ID,而不需要在Doctrine
中获取整个对象解决!