Requiero realizar una consulta a la base de datos en Symfony, tengo una entity que se llama product_insumo, la cual esta relacionada one-to-many con la tabla product e insummo y necesito realizar una consulta que me traiga todos los insumos que tiene en el mismo product y que me traiga sus respectivas cantidades.
**entity Product**
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nombre;
/**
* @ORM\Column(type="float")
*/
private $precio;
/**
* @ORM\Column(type="boolean")
*/
private $estado;
/**
* @ORM\Column(type="date")
*/
private $ingreso;
/**
* @ORM\OneToMany(targetEntity=ProductInsumo::class, mappedBy="Product")
*/
private $productInsumos;
public function __construct()
{
$this->insumo = new ArrayCollection();
$this->productInsumos = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNombre(): ?string
{
return $this->nombre;
}
public function setNombre(string $nombre): self
{
$this->nombre = $nombre;
return $this;
}
public function getPrecio(): ?float
{
return $this->precio;
}
public function setPrecio(float $precio): self
{
$this->precio = $precio;
return $this;
}
public function getEstado(): ?bool
{
return $this->estado;
}
public function setEstado(bool $estado): self
{
$this->estado = $estado;
return $this;
}
public function getIngreso(): ?\DateTimeInterface
{
return $this->ingreso;
}
public function setIngreso(\DateTimeInterface $ingreso): self
{
$this->ingreso = $ingreso;
return $this;
}
/**
* @return Collection|ProductInsumo[]
*/
public function getProductInsumos(): Collection
{
return $this->productInsumos;
}
public function addProductInsumo(ProductInsumo $productInsumo): self
{
if (!$this->productInsumos->contains($productInsumo)) {
$this->productInsumos[] = $productInsumo;
$productInsumo->setProduct($this);
}
return $this;
}
public function removeProductInsumo(ProductInsumo $productInsumo): self
{
if ($this->productInsumos->contains($productInsumo)) {
$this->productInsumos->removeElement($productInsumo);
// set the owning side to null (unless already changed)
if ($productInsumo->getProduct() === $this) {
$productInsumo->setProduct(null);
}
}
return $this;
}
**entity Insumo**
namespace App\Entity;
use App\Repository\InsumoRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=InsumoRepository::class)
*/
class Insumo
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nombre;
/**
* @ORM\Column(type="float")
*/
private $cantidad;
/**
* @ORM\Column(type="string", length=255)
*/
private $medida;
/**
* @ORM\Column(type="boolean")
*/
private $estado;
/**
* @ORM\Column(type="date")
*/
private $ingreso;
/**
* @ORM\OneToMany(targetEntity=ProductInsumo::class, mappedBy="Insumo")
*/
private $productInsumos;
public function __construct()
{
$this->productInsumos = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNombre(): ?string
{
return $this->nombre;
}
public function setNombre(string $nombre): self
{
$this->nombre = $nombre;
return $this;
}
public function getCantidad(): ?float
{
return $this->cantidad;
}
public function setCantidad(float $cantidad): self
{
$this->cantidad = $cantidad;
return $this;
}
public function getMedida(): ?string
{
return $this->medida;
}
public function setMedida(string $medida): self
{
$this->medida = $medida;
return $this;
}
public function getEstado(): ?bool
{
return $this->estado;
}
public function setEstado(bool $estado): self
{
$this->estado = $estado;
return $this;
}
public function getIngreso(): ?\DateTimeInterface
{
return $this->ingreso;
}
public function setIngreso(\DateTimeInterface $ingreso): self
{
$this->ingreso = $ingreso;
return $this;
}
/**
* @return Collection|ProductInsumo[]
*/
public function getProductInsumos(): Collection
{
return $this->productInsumos;
}
public function addProductInsumo(ProductInsumo $productInsumo): self
{
if (!$this->productInsumos->contains($productInsumo)) {
$this->productInsumos[] = $productInsumo;
$productInsumo->setInsumo($this);
}
return $this;
}
public function removeProductInsumo(ProductInsumo $productInsumo): self
{
if ($this->productInsumos->contains($productInsumo)) {
$this->productInsumos->removeElement($productInsumo);
// set the owning side to null (unless already changed)
if ($productInsumo->getInsumo() === $this) {
$productInsumo->setInsumo(null);
}
}
return $this;
}
**entity Product_insumo**
namespace App\Entity;
use App\Repository\ProductInsumoRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ProductInsumoRepository::class)
*/
class ProductInsumo
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="ProductInsumos")
*/
private $Product;
/**
* @ORM\ManyToOne(targetEntity=Insumo::class, inversedBy="ProductInsumos")
*/
private $insumo;
/**
* @ORM\Column(type="float")
*/
private $cantidad;
public function getId(): ?int
{
return $this->id;
}
public function getProduct(): ?Product
{
return $this->Product;
}
public function setProduct(?Product $Product): self
{
$this->Product = $Product;
return $this;
}
public function getInsumo(): ?Insumo
{
return $this->insumo;
}
public function setInsumo(?Insumo $insumo): self
{
$this->insumo = $insumo;
return $this;
}
public function getCantidad(): ?float
{
return $this->cantidad;
}
public function setCantidad(float $cantidad): self
{
$this->cantidad = $cantidad;
return $this;
}
product_id
,insumo_id
,cantidad
FROMproduct_insumo
WHEREproduct_id
=1, pero al pasarla a symfony no me funciona, me dice que el product_id no existe o no esta iniciado.