Home > OS >  Object of class App\Entity\Fournisseurs could not be converted to string
Object of class App\Entity\Fournisseurs could not be converted to string

Time:01-05

I have the following error: Object of class App\Entity\Fournisseurs could not be converted to string I wish that in my order page we can choose a supplier to register in the db page and supplier page in order to associate an order with one or several suppliers I have however used the to_string command well so I do not see where the error is even if it must surely be stupid. I have try add to_sting command in orders.php and in fournisseurs.php ( fournisseurs = suppliers in french)

I use Symfony and EasyAdmin

OrdersCrudController.php

<?php

namespace App\Controller\Admin;

use App\Entity\Orders;
use App\Entity\Fournisseurs;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use phpDocumentor\Reflection\Types\Boolean;

class OrdersCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Orders::class;
        
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
        ->setEntityLabelInPlural('Liste des Commandes');
    }
    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->hideOnForm(),
            NumberField::new('numero_cmd'),
            AssociationField::new('fournisseur','name_sct')
            ->setCrudController(FournisseursCrudController::class),
            DateTimeField::new('date_cmd'),
            DateTimeField::new('date_rcp'),
            TextField::new('article'),
            TextField::new('designation'),
            NumberField::new('qte_cmd_uom'),
            NumberField::new('unite_cmd'),
        ];
    }


    
}

FournisseursCrudController.php

<?php

namespace App\Controller\Admin;

use App\Entity\Fournisseurs;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TelephoneField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class FournisseursCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Fournisseurs::class;
        
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
        ->setEntityLabelInPlural('Liste des Fournisseurs');
    }
    
    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->hideOnForm(),
            TextField::new('name_sct'),
            TextField::new('rcs'),
            EmailField::new('Email'),
            TelephoneField::new('tel'),
            TextField::new('adresse'),
            TextField::new('cp'),
            TextField::new('ville')->setRequired(true),
            
        ];
    }

    
}


Orders.php

<?php

namespace App\Entity;

use App\Repository\OrdersRepository;
use App\Repository\FournisseurRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: OrdersRepository::class)]
class Orders
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $numero_cmd = null;

    #[ORM\ManyToMany(targetEntity: Fournisseurs::class, inversedBy: 'fournisseurs_orders')]
    private Collection $fournisseur;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $date_cmd = null;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $date_rcp = null;

    #[ORM\Column(length: 255)]
    private ?string $article = null;

    #[ORM\Column(length: 255)]
    private ?string $designation = null;

    #[ORM\Column(length: 255)]
    private ?string $qte_cmd_uom = null;

    #[ORM\Column(length: 255)]
    private ?string $unite_cmd = null;

    

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

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

    public function getNumeroCmd(): ?string
    {
        return $this->numero_cmd;
    }

    public function setNumeroCmd(string $numero_cmd): self
    {
        $this->numero_cmd = $numero_cmd;

        return $this;
    }

    /**
     * @return Collection<int, Fournisseurs>
     */
    public function getFournisseur(): Collection
    {
        return $this->fournisseur;
    }

    public function addFournisseur(Fournisseurs $fournisseur): self
    {
        if (!$this->fournisseur->contains($fournisseur)) {
            $this->fournisseur->add($fournisseur);
        }

        return $this;
    }

    public function removeFournisseur(Fournisseurs $fournisseur): self
    {
        $this->fournisseur->removeElement($fournisseur);

        return $this;
    }

    public function getDateCmd(): ?\DateTimeInterface
    {
        return $this->date_cmd;
    }

    public function setDateCmd(\DateTimeInterface $date_cmd): self
    {
        $this->date_cmd = $date_cmd;

        return $this;
    }

    public function getDateRcp(): ?\DateTimeInterface
    {
        return $this->date_rcp;
    }

    public function setDateRcp(\DateTimeInterface $date_rcp): self
    {
        $this->date_rcp = $date_rcp;

        return $this;
    }

    public function getArticle(): ?string
    {
        return $this->article;
    }

    public function setArticle(string $article): self
    {
        $this->article = $article;

        return $this;
    }

    public function getDesignation(): ?string
    {
        return $this->designation;
    }

    public function setDesignation(string $designation): self
    {
        $this->designation = $designation;

        return $this;
    }

    public function getQteCmdUom(): ?string
    {
        return $this->qte_cmd_uom;
    }

    public function setQteCmdUom(string $qte_cmd_uom): self
    {
        $this->qte_cmd_uom = $qte_cmd_uom;

        return $this;
    }

    public function getUniteCmd(): ?string
    {
        return $this->unite_cmd;
    }

    public function setUniteCmd(string $unite_cmd): self
    {
        $this->unite_cmd = $unite_cmd;

        return $this; 
    }

  
    public function __toString()
    {
        return $this->sct_name;
    }
   
}

Fournisseurs.php

<?php

namespace App\Entity;

use App\Repository\FournisseursRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: FournisseursRepository::class)]
class Fournisseurs
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name_sct = null;

    #[ORM\Column(length: 255)]
    private ?string $rcs = null;

    #[ORM\Column(length: 255)]
    private ?string $adresse = null;

    #[ORM\Column(length: 255)]
    private ?string $email = null;

    #[ORM\Column(length: 8)]
    private ?string $cp = null;

    #[ORM\Column(length: 8)]
    private ?string $tel = null;

    #[ORM\OneToMany(mappedBy: 'fournisseur_name', targetEntity: Orders::class)]
    private Collection $orders;

    #[ORM\ManyToMany(targetEntity: Orders::class, mappedBy: 'fournisseur_name')]
    private Collection $orders_obj;

    #[ORM\ManyToMany(targetEntity: Orders::class, mappedBy: 'fournisseur')]
    private Collection $fournisseurs_orders;

    #[ORM\Column(length: 255)]
    private ?string $Ville = null;

    public function __construct()
    {
        $this->orders = new ArrayCollection();
        $this->orders_obj = new ArrayCollection();
        $this->fournisseurs_orders = new ArrayCollection();
    }

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

    public function getNameSct(): ?string
    {
        return $this->name_sct;
    }

    public function setNameSct(string $name_sct): self
    {
        $this->name_sct = $name_sct;

        return $this;
    }

    public function getRcs(): ?string
    {
        return $this->rcs;
    }

    public function setRcs(string $rcs): self
    {
        $this->rcs = $rcs;

        return $this;
    }

    public function getAdresse(): ?string
    {
        return $this->adresse;
    }

    public function setAdresse(string $adresse): self
    {
        $this->adresse = $adresse;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getCp(): ?string
    {
        return $this->cp;
    }

    public function setCp(string $cp): self
    {
        $this->cp = $cp;

        return $this;
    }

    public function getTel(): ?string
    {
        return $this->tel;
    }

    public function setTel(string $tel): self
    {
        $this->tel = $tel;

        return $this;
    }

    /**
     * @return Collection<int, Orders>
     */
    public function getOrders(): Collection
    {
        return $this->orders;
    }

    public function addOrder(Orders $order): self
    {
        if (!$this->orders->contains($order)) {
            $this->orders->add($order);
            $order->setFournisseurName($this);
        }

        return $this;
    }

    public function removeOrder(Orders $order): self
    {
        if ($this->orders->removeElement($order)) {
            // set the owning side to null (unless already changed)
            if ($order->getFournisseurName() === $this) {
                $order->setFournisseurName(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Orders>
     */
    public function getOrdersObj(): Collection
    {
        return $this->orders_obj;
    }

    public function addOrdersObj(Orders $ordersObj): self
    {
        if (!$this->orders_obj->contains($ordersObj)) {
            $this->orders_obj->add($ordersObj);
            $ordersObj->addFournisseurName($this);
        }

        return $this;
    }

    public function removeOrdersObj(Orders $ordersObj): self
    {
        if ($this->orders_obj->removeElement($ordersObj)) {
            $ordersObj->removeFournisseurName($this);
        }

        return $this;
    }

    /**
     * @return Collection<int, Orders>
     */
    public function getFournisseursOrders(): Collection
    {
        return $this->fournisseurs_orders;
    }

    public function addFournisseursOrder(Orders $fournisseursOrder): self
    {
        if (!$this->fournisseurs_orders->contains($fournisseursOrder)) {
            $this->fournisseurs_orders->add($fournisseursOrder);
            $fournisseursOrder->addFournisseur($this);
        }

        return $this;
    }

    public function removeFournisseursOrder(Orders $fournisseursOrder): self
    {
        if ($this->fournisseurs_orders->removeElement($fournisseursOrder)) {
            $fournisseursOrder->removeFournisseur($this);
        }

        return $this;
    }

    public function getVille(): ?string
    {
        return $this->Ville;
    }

    public function setVille(string $Ville): self
    {
        $this->Ville = $Ville;

        return $this;
    }

}

CodePudding user response:

Try to move your function

public function __toString()
{
    return $this->sct_name;
}

in Fournisseurs.php instead of Orders.php and also rename

$this->sct_name

To

$this->name_sct

CodePudding user response:

You have to add toString function on Fournisseur. Your orders class has a relation with fournisseur, and symfony can't know what to display on your order form for the fournisseur input. So you have to explain in Fournisseur.php what is the string to show on the input.

public function __toString()
{
   return $this->name;
}   
  • Related