I got this error when I go to /products/purchases to get the list of all my purchases but I got this error
Object of class DoctrineORMPersistentCollection could not be converted to string
this is my twig
{% extends 'admin/admin.html.twig' %}
{% block title %}Hello HomeController!{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" href={{asset('styles/css/purchases.css')}}>
{% endblock %}
{% block main %}
<div class="add">
<a href="{{path('purchase_add')}}" class="btn btn-primary">Ajouter une Vente</a>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Nom de Produit</th>
<th scope="col">Quantité</th>
</tr>
</thead>
<tbody>
{% for purchase in purchases %}
<tr>
<th scope="row">{{purchase.id}}</th>
<td>{{purchase.nameproduct}}</td>
<td>{{purchase.quantity}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
this is my Purchase entity
<?php
namespace AppEntity;
use AppRepositoryPurchaseRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: PurchaseRepository::class)]
class Purchase
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
/**
* @var Collection<int, Product>
*/
#[ORMOneToMany(targetEntity: Product::class, mappedBy: 'purchase')]
#[ORMJoinColumn(name : "name_product", referencedColumnName : "name")]
private Collection $name_product;
#[ORMColumn]
private ?int $quantity = null;
public function __construct()
{
$this->name_product = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection<int, Product>
*/
public function getNameProduct(): Collection
{
return $this->name_product;
}
public function addNameProduct(Product $nameProduct): static
{
if (!$this->name_product->contains($nameProduct)) {
$this->name_product->add($nameProduct);
$nameProduct->setPurchase($this);
}
return $this;
}
public function removeNameProduct(Product $nameProduct): static
{
if ($this->name_product->removeElement($nameProduct)) {
// set the owning side to null (unless already changed)
if ($nameProduct->getPurchase() === $this) {
$nameProduct->setPurchase(null);
}
}
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): static
{
$this->quantity = $quantity;
return $this;
}
public function __toString()
{
return $this->name_product; // Retourne le nom de la catégorie comme chaîne
}
}
and this is my Product entity
<?php
namespace AppEntity;
use AppRepositoryProductRepository;
use DoctrineDBALTypesTypes;
use DoctrineORMMapping as ORM;
use AppEntityCategory;
#[ORMEntity(repositoryClass: ProductRepository::class)]
class Product
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(length: 255)]
private ?string $name = null;
#[ORMColumn(type: Types::TEXT)]
private ?string $description = null;
#[ORMColumn]
private ?int $quantity = null;
#[ORMColumn]
private ?int $price = null;
#[ORMColumn]
private ?DateTimeImmutable $created_at = null;
#[ORMColumn]
private ?DateTimeImmutable $modified_at = null;
#[ORMColumn(length: 255)]
private ?string $status = null;
#[ORMColumn(type: Types::TEXT)]
private ?string $image_name = null;
#[ORMManyToOne(inversedBy: 'products')]
#[ORMJoinColumn(nullable: false)]
private ?Category $category = null;
#[ORMManyToOne(inversedBy: 'name_product')]
private ?Purchase $purchase = null;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): static
{
$this->description = $description;
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): static
{
$this->quantity = $quantity;
return $this;
}
public function getPrice(): ?int
{
return $this->price;
}
public function setPrice(int $price): static
{
$this->price = $price;
return $this;
}
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->created_at;
}
public function setCreatedAt(DateTimeImmutable $created_at): static
{
$this->created_at = $created_at;
return $this;
}
public function getModifiedAt(): ?DateTimeImmutable
{
return $this->modified_at;
}
public function setModifiedAt(DateTimeImmutable $modified_at): static
{
$this->modified_at = $modified_at;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): static
{
$this->status = $status;
return $this;
}
public function getImageName(): ?string
{
return $this->image_name;
}
public function setImageName(string $image_name): static
{
$this->image_name = $image_name;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): static
{
$this->category = $category;
return $this;
}
public function getPurchase(): ?Purchase
{
return $this->purchase;
}
public function setPurchase(?Purchase $purchase): static
{
$this->purchase = $purchase;
return $this;
}
public function __toString()
{
return $this->name; // Retourne le nom de la catégorie comme chaîne
}
}
I got error when I want to print all the list of purchases
New contributor
Helmi Mehdi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.