簡體   English   中英

Symfony 5 錯誤,屬性訪問有問題

[英]Symfony 5 Error, I have problem on properties access

我收到此錯誤,我不知道問題出在哪里

屬性“image_medecin”和方法之一“image_medecin()”、“getimage_medecin()”/“isimage_medecin()”/“hasimage_medecin()”或“__call()”都不存在並且在 class“App\實體\醫學”

問題是我想在視圖中顯示醫生圖像 < templates\medecin\show.html.twig > 我正在從 ZA8CFDE6331BD59EB2AC96F8911Bd59EB2AC96F8911C4B666Z medecin中讀取圖像問題,但它的所有屬性屬性都是圖像醫生顯示的數據不,我不知道為什么,請大家幫忙,我被困了幾個小時!

在此處檢查錯誤圖像

檢查我的實體醫生:

<?php

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: MedecinRepository::class)]
class Medecin
{
    public string $UPLOAD_FOLDER = "C:\Users\\email\OneDrive\Bureau\Symfony\clinique\public\assets\Uploads";

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column()]
    private ?int $id = null;

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

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $experience = null;

    #[ORM\Column]
    private ?float $salaire = null;

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

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

    #[ORM\Column(length: 200 , nullable: true)]
    private ?string $image_medecin = null;

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

    #[ORM\OneToOne(mappedBy: 'fk_medecin', cascade: ['persist', 'remove'])]
    private ?User $user = null;

    #[ORM\ManyToMany(targetEntity: Dossier::class, mappedBy: 'fk_medecin')]
    private Collection $dossiers;

    #[ORM\ManyToMany(targetEntity: Specialite::class, inversedBy: 'medecins')]
    private Collection $fk_specialite;

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

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

    public function getMatricule(): ?string
    {
        return $this->matricule;
    }

    public function setMatricule(string $matricule): self
    {
        $this->matricule = $matricule;

        return $this;
    }

    public function getExperience(): ?string
    {
        return $this->experience;
    }

    public function setExperience(?string $experience): self
    {
        $this->experience = $experience;

        return $this;
    }

    public function getSalaire(): ?float
    {
        return $this->salaire;
    }

    public function setSalaire(float $salaire): self
    {
        $this->salaire = $salaire;

        return $this;
    }

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

    public function setTempsTravail(\DateTimeInterface $temps_travail): self
    {
        $this->temps_travail = $temps_travail;

        return $this;
    }

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

    public function setJourTravail(\DateTimeInterface $jour_travail): self
    {
        $this->jour_travail = $jour_travail;

        return $this;
    }

    public function getImageMedecin(): ?string
    {
        return $this->image_medecin;
    }

    public function setImageMedecin(string|null $image_medecin): self
    {
        $this->image_medecin = $image_medecin;

        return $this;
    }

    
    public function getStatusMedecin(): ?string
    {
        return $this->status_medecin;
    }

    public function setStatusMedecin(string $status_medecin): self
    {
        $this->status_medecin = $status_medecin;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        // unset the owning side of the relation if necessary
        if ($user === null && $this->user !== null) {
            $this->user->setFkMedecin(null);
        }

        // set the owning side of the relation if necessary
        if ($user !== null && $user->getFkMedecin() !== $this) {
            $user->setFkMedecin($this);
        }

        $this->user = $user;

        return $this;
    }

    /**
     * @return Collection<int, Dossier>
     */
    public function getDossiers(): Collection
    {
        return $this->dossiers;
    }

    public function addDossier(Dossier $dossier): self
    {
        if (!$this->dossiers->contains($dossier)) {
            $this->dossiers[] = $dossier;
            $dossier->addFkMedecin($this);
        }

        return $this;
    }

    public function removeDossier(Dossier $dossier): self
    {
        if ($this->dossiers->removeElement($dossier)) {
            $dossier->removeFkMedecin($this);
        }
        
        return $this;
    }

    /**
     * @return Collection<int, Specialite>
     */
    public function getFkSpecialite(): Collection
    {
        return $this->fk_specialite;
    }

    public function addFkSpecialite(Specialite $fkSpecialite): self
    {
        if (!$this->fk_specialite->contains($fkSpecialite)) {
            $this->fk_specialite[] = $fkSpecialite;
        }

        return $this;
    }

    public function removeFkSpecialite(Specialite $fkSpecialite): self
    {
        $this->fk_specialite->removeElement($fkSpecialite);

        return $this;
    }
}

在(顯示方法)中檢查我的 Controller:

<?php

namespace App\Controller;

use App\Entity\Medecin;
use App\Entity\User;
use App\Form\MedecinType;
use App\Form\UserType;
use App\Repository\UserRepository;
use App\Repository\MedecinRepository;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;


#[Route('/medecin')]
class MedecinController extends AbstractController
{
    #[Route('/', name: 'app_medecin_index', methods: ['GET'])]
    public function index(MedecinRepository $medecinRepository): Response
    {
    //    ["0" => "Inactive" , "1" => "Active" , "2" => "Malade" , "3" => "En Congé"] 
        $medecinData = $medecinRepository->findAllDoctors();
        return $this->render('medecin/test.html.twig', [
            'medecins' => $medecinData 
        ]);
    }

    #[Route('/new', name: 'app_medecin_new', methods: ['GET', 'POST'])]
    public function new(Request $request, MedecinRepository $medecinRepository , UserRepository $userRepository , UserPasswordHasherInterface $passwordHasher): Response
    {
        // Creating The User Form will be created From the class UserType Generator Form To link the Medecin Data to the User Entity Related With The Foreign Key
        $user = new User();
        $formUser = $this->createForm(UserType::class, $user);
        $formUser->remove('user_role');
        $formUser->handleRequest($request);
        // Creating The Medecin Form will be created From the class MedecinType Generator Form 
        $medecin = new Medecin();
        $formMedecin = $this->createForm(MedecinType::class, $medecin);
        $formMedecin->handleRequest($request);

        $plaintextPassword = ''; // get the plain password from the form

        if ($formMedecin->isSubmitted() && $formMedecin->isValid()) {

                // Setting the value null by default for the medecin Image
                $medecin -> setImageMedecin(null);
                // if this condition is not true than it means a file has uploaded , not an empty field file input .
                  if(!($_FILES['medecin']['error']['image_medecin'] == UPLOAD_ERR_NO_FILE)) {
                    // This file superglobal gets all the information from the file that we want to upload using an input from a form
                    $photo = $_FILES['medecin'];
                    // $_files array contains  : name/ type / tmp_name / error / size
                    $fileName = $photo['name']['image_medecin'];
                    $fileTmpName = $photo['tmp_name']['image_medecin'];
                    $fileSize = $photo['size']['image_medecin'];
                    $fileError = $photo['error']['image_medecin'];
                
                    // to get the extension of the file
                    $fileExt = explode('.', $fileName);
                    // Make sure that always the extension comes in small letters
                    $fileActualExt = strtolower(end($fileExt));
                
                    // inside this array we gonna tell it which type of files we want to allow inside the website
                    $allowed = array('jpg' , 'jpeg' , 'png' , 'webp');
                
                    if(in_array($fileActualExt , $allowed)) {
                        // if the file error is equal to 0 that means that we had no erros uploading this file 
                        if($fileError == 0){
                
                            if($fileSize < 5000000){

                                $fileNameNew = "doctor".uniqid().".". $fileActualExt;
                                $fileDestination = $medecin-> UPLOAD_FOLDER  . "/medecin/" . $fileNameNew ;
                                move_uploaded_file($fileTmpName,$fileDestination);
                                $medecin->setImageMedecin($fileNameNew);
        
                            } else {
                                echo "Your file is too big !";
                                exit ;
                            }
                
                            } else {
                            echo "There was an error uploading your file !";
                            exit ;
                            }
                
                    } else {
                        echo "You can not upload files of this type !";
                        exit ;
                    }
                }              
            // Making Sure that the matricule is UpperCase
            strtoupper($medecin->getMatricule());    
            // Adding the medecin to the database
            $medecinRepository->add($medecin, true);

            $user -> setFkMedecin($medecin);
            strtoupper($user -> getCin());
            $user -> setUserRole('ROLE_MEDECIN');
            $hashedPassword = $passwordHasher->hashPassword(
                $user,
                $plaintextPassword
            );
            
            $user->setPassword($hashedPassword);
            $userRepository->add($user, true);

            return $this->redirectToRoute('app_medecin_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('medecin/new.html.twig', [
            'form' => $formUser ,
            'medecinForm' => $formMedecin
        ]);
    }

    #[Route('/{id}', name: 'app_medecin_show', methods: ['GET'])]
    public function show(Medecin $medecin , UserRepository $userRepository): Response
    {
        // echo "<pre>"; var_dump($medecin); echo"</pre>"; exit ;
        return $this->render('medecin/show.html.twig', [
            'medecin' => $medecin
        ]);
    }

    #[Route('/{id}/edit', name: 'app_medecin_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, int $id , Medecin $medecin, User $user, MedecinRepository $medecinRepository): Response
    {
        $formMedecin = $this->createForm(MedecinType::class, $medecin);
        $formMedecin->handleRequest($request);

        $formUser = $this->createForm(UserType::class, $user);
        $formUser->remove('user_role');
        $formUser->handleRequest($request);

        if ($formMedecin->isSubmitted() && $formMedecin->isValid()) {
            $medecinRepository->add($medecin, true);


            return $this->redirectToRoute('app_medecin_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('medecin/edit.html.twig', [
            'medecin' => $medecin,
            'form' => $formUser,
            'medecinForm' => $formMedecin
        ]);
    }

    #[Route('/{id}/delete', name: 'app_medecin_delete', methods: ['POST'])]
    public function delete(Request $request, Medecin $medecin, MedecinRepository $medecinRepository): Response
    {
        if ($this->isCsrfTokenValid('delete'.$medecin->getId(), $request->request->get('_token'))) {
            $medecinRepository->remove($medecin, true);
        }
        return $this->redirectToRoute('app_medecin_index', [], Response::HTTP_SEE_OTHER);
    }
}

這是視圖行( 34 (發生錯誤的位置)):

{% extends 'base.html.twig' %}

{% block title %}Medecin{% endblock %}

{% block body %}
    <h1>Medecin</h1>
{{ dump(medecin) }}

    <a href="{{ path('app_medecin_index') }}">back to list</a>

    <a href="{{ path('app_medecin_edit', {'id': medecin.id}) }}">edit</a>

    {{ include('medecin/_delete_form.html.twig') }}


        <div class="page-wrapper">
            <div class="content">
                <div class="row">
                    <div class="col-sm-7 col-6">
                        <h4 class="page-title">My Profile</h4>
                    </div>

                    <div class="col-sm-5 col-6 text-right m-b-30">
                        <a href="edit-profile.html" class="btn btn-primary btn-rounded"><i class="fa fa-plus"></i> Edit Profile</a>
                    </div>
                </div>
                <div class="card-box profile-header">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="profile-view">
                                <div class="profile-img-wrap">
                                    <div class="profile-img">
                                    
                                <img src="{{ medecin.image_medecin ? "asset('assets/Uploads/medecin/')" ~ medecin.image_medecin : "https://img.freepik.com/free-photo/doctor-with-his-arms-crossed-white-background_1368-5790.jpg?w=2000"}}" alt="Medecin Image">
                                    </div>
                                </div>
                                <div class="profile-basic">
                                    <div class="row">
                                        <div class="col-md-5">
                                            <div class="profile-info-left">
                                                <h3 class="user-name m-t-0 mb-0">{{ medecin.user.nom ~ ' ' ~ medecin.user.prenom }}</h3>
                                                <small class="text-muted">Gynecologist</small>
                                                <div class="staff-id">Employee ID : {{ medecin.matricule }}</div>
                                                <div class="staff-msg"><a href="chat.html" class="btn btn-primary">Send Message {{ medecin.matricule }}</a></div>
                                            </div>
                                        </div>
                                        <div class="col-md-7">
                                            <ul class="personal-info">
                                                <li>
                                                    <span class="title">Phone:</span>
                                                    <span class="text"><a href="#">{{ medecin.user.telephone }}</a></span>
                                                </li>
                                                <li>
                                                    <span class="title">Email:</span>
                                                    <span class="text"><a href="#">{{ medecin.user.email }}</a></span>
                                                </li>
                                                <li>
                                                    <span class="title">Age:</span>
                                                    <span class="text">{{ medecin.user.age }}</span>
                                                </li>
                                                <li>
                                                    <span class="title">Address:</span>
                                                    <span class="text">{{ medecin.user.address }}</span>
                                                </li>
                                                <li>
                                                    <span class="title">Gender:</span>
                                                    <span class="text">Male</span>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>                        
                        </div>
                    </div>
                </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div class="card-box mb-0" id="experienceCard">
                            <h3 class="card-title">Experience</h3>
                            <div class="experience-box">
                                <ul class="experience-list">
                                    <li>
                                        <div class="experience-user">
                                            <div class="before-circle"></div>
                                        </div>
                                        <div class="experience-content">
                                            <div class="timeline-content">
                                                <a href="#/" class="name">Consultant Gynecologist</a>
                                                <span class="time">Jan 2014 - Present (4 years 8 months)</span>
                                            </div>
                                        </div>
                                    </li>
                                    <li>
                                        <div class="experience-user">
                                            <div class="before-circle"></div>
                                        </div>
                                        <div class="experience-content">
                                            <div class="timeline-content">
                                                <a href="#/" class="name">Consultant Gynecologist</a>
                                                <span class="time">Jan 2009 - Present (6 years 1 month)</span>
                                            </div>
                                        </div>
                                    </li>
                                    {# <li>
                                        <div class="experience-user">
                                            <div class="before-circle"></div>
                                        </div>
                                        <div class="experience-content">
                                            <div class="timeline-content">
                                                <a href="#/" class="name">Consultant Gynecologist</a>
                                                <span class="time">Jan 2004 - Present (5 years 2 months)</span>
                                            </div>
                                        </div>
                                    </li> #}
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                        </div>
                        <div class="tab-pane" id="bottom-tab2">
                            Tab content 2
                        </div>
                        <div class="tab-pane" id="bottom-tab3">
                            Tab content 3
                        </div>
                    </div>
                </div>
            </div>

        </div>
{% endblock %}

錯誤非常明確

屬性“image_medecin”和方法之一“image_medecin()”、“getimage_medecin()”/“isimage_medecin()”/“hasimage_medecin()”或“__call()”都不存在並且在 class“App\實體\醫學”

Twig 嘗試訪問image_medecin中的屬性medecin但它是私有的,不能直接訪問。

所以它嘗試image_medecin()getimage_medecinisimage_medecin()hasimage_medecin()和最后的魔術方法__call()

您可以直接在您的 twig 模板中使用正確的方法:

<img src="{{ medecin.getImageMedecin() ? "asset('assets/Uploads/medecin/')" ~ medecin.getImageMedecin() : "https://img.freepik.com/free-photo/doctor-with-his-arms-crossed-white-background_1368-5790.jpg?w=2000"}}" alt="Medecin Image">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM