skip to Main Content

I have the following error: Object of class AppEntityFournisseurs 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 AppControllerAdmin;

use AppEntityOrders;
use AppEntityFournisseurs;
use DateTime;
use DoctrineORMEntityManagerInterface;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldAssociationField;
use EasyCorpBundleEasyAdminBundleFieldDateTimeField;
use EasyCorpBundleEasyAdminBundleFieldIdField;
use EasyCorpBundleEasyAdminBundleFieldNumberField;
use EasyCorpBundleEasyAdminBundleFieldTextField;
use phpDocumentorReflectionTypesBoolean;

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 AppControllerAdmin;

use AppEntityFournisseurs;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldEmailField;
use EasyCorpBundleEasyAdminBundleFieldIdField;
use EasyCorpBundleEasyAdminBundleFieldTelephoneField;
use EasyCorpBundleEasyAdminBundleFieldTextField;

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 AppEntity;

use AppRepositoryOrdersRepository;
use AppRepositoryFournisseurRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineDBALTypesTypes;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: OrdersRepository::class)]
class Orders
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

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

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

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

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

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

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

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

    #[ORMColumn(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 AppEntity;

use AppRepositoryFournisseursRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: FournisseursRepository::class)]
class Fournisseurs
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

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

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

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

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

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

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

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

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

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

    #[ORMColumn(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;
    }

}

2

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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;
    }   
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search