Home > database >  Why can not create new product when i use EasyAdmin on Symfony
Why can not create new product when i use EasyAdmin on Symfony

Time:11-23

I install EasyAdmin Bundle for my backend web service, but when i want to create a new instance on the crud controller page, i got a error. enter image description here There is my entities

/**
     * @var Tipoproducto
     *
     * @ORM\ManyToOne(targetEntity="Tipoproducto", inversedBy="id")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="idTipo", referencedColumnName="id")
     * })
     */
    private $idtipo;

    /**
     * @var Precio
     *
     * @ORM\ManyToOne(targetEntity="Precio")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="idPrecio", referencedColumnName="id")
     * })
     */
    private $idprecio;

And there is my crud controller code

public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->hideOnForm(),
            TextField::new('nombre'),
            TextareaField::new('descripcion'),
            IntegerField::new('stock'),
            TextField::new('foto'),
            AssociationField::new('idtipo', 'Tipo'),
            AssociationField::new('idprecio', 'Precio'),
        ];
    }

I dont have idea why i got this error, i followed the offcial page to create my web service

I try to change on my entites because i saw some video, theres entites are diferent but it not work.

CodePudding user response:

i get the solution for this question.
I need to override createEntity() metod to assign a value for my idtipo properties of the new producto. I nned to assign a instance of the entity TipoProducto to it.

public function createEntity(string $entityFqcn)
{
    $product = new Producto();
    $precio=new Precio();
    $tipo=new Tipoproducto();
    $precio=$this->getDoctrine()->getRepository(Precio::class)->findOneBy(['id' => '1']);
    $product->setIdprecio($precio);
    $tipo=$this->getDoctrine()->getRepository(Tipoproducto::class)->findOneBy(['id' => '1']);
    $product->setIdtipo($tipo);
    return $product;
}

Now my page can read this propiertes.
image

  • Related