【问题标题】:Symfony 5 Error, I have problem on properties accessSymfony 5 错误,我的属性访问有问题
【发布时间】:2022-08-19 05:22:50
【问题描述】:

我收到此错误,但我不知道问题出在哪里

属性 \"image_medecin\" 和方法之一 \"image_medecin()\"、\"getimage_medecin()\"/\"isimage_medecin()\"/\"hasimage_medecin()\" 或 \"__call() \" 存在并在类 \"App\\Entity\\Medecin\" 中具有公共访问权限

问题是我想在视图中显示医生图像 <模板\\medecin\\show.html.twig> 我正在读取一个对象医学作为医生的数据,问题是它的所有属性都显示了,但是image_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;
    }
}

检查我的控制器(显示方法):

<?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 %}

    标签: php symfony doctrine-orm twig


    【解决方案1】:

    错误非常明确

    既不是属性“image_medecin”也不是其中一种方法 "image_medecin()", “getimage_medecin()”/“isimage_medecin()”/“hasimage_medecin()”或 “__call()” 存在并在“App\Entity\Medecin”类中具有公共访问权限

    Twig 尝试访问对象medecin 中的属性image_medecin,但它是私有的,无法直接访问。

    所以它尝试了image_medecin()、getimage_medecin、isimage_medecin()、hasimage_medecin(),最后尝试了魔术方法__call()。

    您可以直接在树枝模板中使用正确的方法:

    <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">
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      相关资源
      最近更新 更多