Yot
Yot

Reputation: 5

Doctrine mapping exception even though the class does exist

I m new in symfony4 so sorry if my question is stupid but I work for three days on this problem without finding the solution. So I have an Entity "Professionnel" and an Entity "Coordonnees" and i want render in a view a table with the proffessionals and there location. It already work with two other classes in my application Members and Sponsors. For my Entity "Professionnel", i have a this error: Error return

This is my Entity "Professionnel":

<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity(repositoryClass="App\Repository\ProfessionnelRepository")
 */
class Professionnel
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $nomProfessionnel;

/**
 * @ORM\Column(type="string", length=255)
 */
private $prenomProfessionnel;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $fonctionProfessionnel;

/**
 * @ORM\Column(type="text", nullable=true)
 */
private $observationsProfessionnel;

/**
 * @ORM\Column(type="string", length=50, nullable=true)
 */
private $titreProfessionnel;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Coordonnees")
 */
private $coordonnees;

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

public function getNomProfessionnel(): ?string
{
    return $this->nomProfessionnel;
}

public function setNomProfessionnel(string $NomProfessionnel): self
{
    $this->nomProfessionnel = $NomProfessionnel;

    return $this;
}

public function getPrenomProfessionnel(): ?string
{
    return $this->prenomProfessionnel;
}

public function setPrenomProfessionnel(string $PrenomProfessionnel): self
{
    $this->prenomProfessionnel = $PrenomProfessionnel;

    return $this;
}

public function getFonctionProfessionnel(): ?string
{
    return $this->fonctionProfessionnel;
}

public function setFonctionProfessionnel(?string $FonctionProfessionnel): self
{
    $this->fonctionProfessionnel = $FonctionProfessionnel;

    return $this;
}

public function getObservationsProfessionnel(): ?string
{
    return $this->observationsProfessionnel;
}

public function setObservationsProfessionnel(?string $ObservationsProfessionnel): self
{
    $this->observationsProfessionnel = $ObservationsProfessionnel;

    return $this;
}

public function getTitreProfessionnel(): ?string
{
    return $this->titreProfessionnel;
}

public function setTitreProfessionnel(?string $TitreProfessionnel): self
{
    $this->titreProfessionnel = $TitreProfessionnel;

    return $this;
}

public function getCoordonnees(): ?Coordonnees
{
    return $this->coordonnees;
}

public function setCoordonnees(?Coordonnees $coordonnees): self
{
    $this->coordonnees = $coordonnees;

    return $this;
}
}

My ProfessionnelRepository

<?php
namespace App\Repository;
use App\Entity\Professionnel;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method Professionnel|null find($id, $lockMode = null, $lockVersion =          null)
 * @method Professionnel|null findOneBy(array $criteria, array $orderBy = null)
 * @method Professionnel[]    findAll()
 * @method Professionnel[]    findBy(array $criteria, array $orderBy = null,     $limit = null, $offset = null)
 */
class ProfessionnelRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Professionnel::class);
    }

    public function getProEtAdresses(): array
    {
        $conn = $this->getEntityManager()->getConnection();

        $sql = '
            SELECT * 
            FROM coordonnees 
            INNER JOIN `professionnel`
            ON `professionnel`.`coordonnees_id`= `coordonnees`.`id`
            ';
        $stmt = $conn->prepare($sql);
        $stmt->execute();
}

My controller

<?php
// GestionASM17/src/Controller/ProfessionnelsController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Coordonnees;

class ProfessionnelsController extends Controller
{

    public function afficherProfessionnels()
    {

//Récupère sous forme de tableau les Professionnels et leurs adresses
    $ListeProEtAdresse = $this->getDoctrine()
    ->getRepository(Professionnel::class)
    ->getProEtAdresse();

    return $this->render('GestionASM17/professionnels.html.twig', array(
        'ListeProEtAdresse'=>$ListeProEtAdresse
    ));
}

public function detailProfessionnels($id)
{
//Récupère les données du Professionnel passé en paramètre dans une instance de professionnel
    $Pro = $this->getDoctrine()
    ->getRepository(Professionnel::class)
    ->find($id);  

// Récupère les coordonnées du Professionnel passé en paramètre
    $Coordonnees = $this->getDoctrine()
    ->getRepository(Coordonnees::class)
    ->find($Pro->getCoordonnees());  

//Renvoi de la vue
    return $this->render('GestionASM17/detailProfessionnels.html.twig', array(
        'Pro'=>$Pro ,
        'Coordonnees'=>$Coordonnees
    ));
    }
}

So I don't underrstand why it works with two of my Entities and not with my Entity "Professionnel". Thank you by advance for your answers and excuse my approximative English

Kourilles

Upvotes: 0

Views: 667

Answers (1)

DrKey
DrKey

Reputation: 3495

Most probably it depends on your Professionnel::class calling in ProfessionnelsController. In fact you didn't add any use statement for that class in your controller. This means that said class is going to be loaded from the same namespace of your controller App\Controller but of course there's no Professionnel::class having that namespace.

Therefore you just need to add the proper use statement for that class in your ProfessionnelsController

<?php
// GestionASM17/src/Controller/ProfessionnelsController.php
namespace App\Controller;

...
use App\Entity\Professionnel;

class ProfessionnelsController extends Controller

Upvotes: 2

Related Questions