首页 > 解决方案 > Symfony 错误:该属性和方法之一都不存在并且在类中具有公共访问权限

问题描述

我使用 symfony 3.4。我为我的项目表创建创建、删除、编辑、显示页面..但是尝试显示一个项目时出现此错误 Neither the property "fee" nor one of the methods "fee()", "getfee()"/"isfee()" or "__call()" exist and have public access in class "AppBundle\Entity\Project".

我的实体类(/Entity/Project.php)

<?php
namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity
 * @ORM\Table(name="project")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProjectRepository")
 */
class Project
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Payment", mappedBy="id")
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Conversation", mappedBy="id")
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Attachment", mappedBy="id")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank(message="please fill title")
     * @Assert\Length(min=3,minMessage="message from annotation: field must be at least 3")
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank(message="please fill type")
     */
    private $type;

    /**
     * @ORM\Column(type="integer")
     * @Assert\NotBlank(message="please fill budget")
     */
    private $budget;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\Column(type="datetime")
     * @Assert\DateTime
     */
    private $registerDate;

    /**
     * @ORM\Column(type="datetime")
     * @Assert\DateTime
     */
    private $endDate;

    /**
     * @ORM\Column(type="integer")
     */
    private $prePay;
    /**
     * @ORM\Column(type="integer")
     */
    private $fee;
    /**
     * @ORM\Column(type="integer", length=1)
     */
    private $isPay;
    /**
     * @ORM\Column(type="integer", length=1)
     */
    private $isPrePay;
    /**
     * @ORM\Column(name="attachmentId", type="string")
     *
     * @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
     * @Assert\File(mimeTypes={ "application/zip" })
     */
    private $attachmentId;
    /**
     * @ORM\Column(type="smallint")
     */
    private $status;
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $user;

    public function __construct()
    {
        $this->registerDate = new \DateTime();
        $this->prePay = 0;
        $this->fee = 0;
        $this->isPay = 0;
        $this->isPrePay = 0;
        $this->status = 0; //@todo make this better 
    }
    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getBudget()
    {
        return $this->budget;
    }

    public function setBudget($budget)
    {
        $this->budget = $budget;
    }
    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }

    public function getRegisterDate()
    {
        return $this->registerDate;
    }

    public function setRegisterDate($register_date)
    {
        $this->registerDate = $register_date;
    }

    public function getEndDate()
    {
        return $this->endDate;
    }

    public function setEndDate($end_date)
    {
        $this->endDate = $end_date;
    }

    public function getPrePay()
    {
        return $this->prePay;
    }

    public function setPrePay($pre_pay)
    {
        $this->prePay = $pre_pay;
    }

    public function geFee()
    {
        return $this->fee;
    }

    public function setFee($fee)
    {
        $this->fee = $fee;
    }

    public function getIsPay()
    {
        return $this->isPay;
    }

    public function setIsPay($is_pay)
    {
        $this->isPay = $is_pay;
    }

    public function setIsPrePay($is_prepay)
    {
        $this->isPrePay = $is_prepay;
    }

    public function getIsPrePay()
    {
        return $this->isPrePay;
    }

    public function getAttachment()
    {
        return $this->attachmentId;
    }

    public function setAttachment($attachment)
    {
        $this->attachmentId = $attachment;

        //return $this;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($status)
    {
        $this->status = $status;
    }

    public function setUserId($user_id)
    {
        $this->user = $user_id;
    }

    public function getUserId($user_id)
    {
        return $this->user; 
    }
}



我的项目类型 (Form/ProjectType.php)

<?php

namespace AppBundle\Form;

use AppBundle\Entity\Project;
use AppBundle\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\BaseType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

class ProjectType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title')
        ->add('type')
        ->add('budget',IntegerType::class)
        ->add('description',TextareaType::class)
        ->add('registerDate',DateTimeType::class)
        ->add('endDate',DateTimeType::class)
        ->add('prePay',IntegerType::class)
        ->add('fee',IntegerType::class,array('mapped'=>false))
        ->add('isPay',ChoiceType::class,[
            'choices'  => [
                'Yes' => 1,
                'No' => 0,
            ],
        ])
        ->add('isPrePay',ChoiceType::class,[
            'choices'  => [
                'Yes' => 1,
                'No' => 0,
            ],
        ])
        ->add('attachmentId',NumberType::class,array('mapped'=>false))
        ->add('status',ChoiceType::class,[
            'choices'  => [
                'Pending Review' => 0,
                'Pending PrePaid' => 1,
                'In Process' => 2,
                'End Work' => 3,
                'Complete' => 4
            ],
        ])
        ->add('user',NumberType::class,['mapped'=>false]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Project::class
        ));
    }

    /**
     * {@inheritdoc}
     */
    /*public function getBlockPrefix()
    {
        return 'appbundle_project';
    }*/




}

我的项目控制器文件( src/AppBundle/Controller/ProjectCntroller.php )

<?php
/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace AppBundle\Controller\Admin;

use AppBundle\Entity\Project;
use AppBundle\Form\ProjectType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

/**
 * Project controller.
 *
 * @Route("/admin", name="admin_project_controller")
 * @Security("has_role('ROLE_ADMIN')")
 */
class ProjectController extends Controller
{
    /**
     * Lists all project entities.
     *
     * @Route("/project", name="admin_project_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $projects = $em->getRepository(Project::class)->findAll();

        return $this->render('admin/project/index.html.twig', array(
            'projects' => $projects,
        ));
    }

    /**
     * Creates a new project entity.
     *
     * @Route("/project/new", name="admin_project_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $project = new Project();
        $form = $this->createForm(ProjectType::class, $project);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($project);
            $em->flush();

            return $this->redirectToRoute('admin_project_show', array('id' => $project->getId()));
        }

        return $this->render('admin/project/new.html.twig', array(
            'project' => $project,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a project entity.
     *
     * @Route("/project/{id}", name="admin_project_show")
     * @Method("GET")
     */
    public function showAction(Project $project)
    {
        $deleteForm = $this->createDeleteForm($project);

        return $this->render('admin/project/show.html.twig', array(
            'project' => $project,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing project entity.
     *
     * @Route("/project/{id}/edit", name="admin_project_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Project $project)
    {
        $deleteForm = $this->createDeleteForm($project);
        $editForm = $this->createForm(ProjectType::class, $project);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('admin_project_edit', array('id' => $project->getId()));
        }

        return $this->render('admin/project/edit.html.twig', array(
            'project' => $project,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a project entity.
     *
     * @Route("/project/{id}", name="admin_project_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Project $project)
    {
        $form = $this->createDeleteForm($project);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($project);
            $em->flush();
        }

        return $this->redirectToRoute('admin_project_index');
    }

    /**
     * Creates a form to delete a project entity.
     *
     * @param Project $project The project entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Project $project)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('admin_project_delete', array('id' => $project->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}

我的 index.twig 文件(apop/Resource/views/admins/project/index.html.twig)

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

{% block body %}
    <h1>Projects list</h1>

    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Title</th>
                <th>Type</th>
                <th>Budget</th>
                <th>Description</th>
                <th>Registerdate</th>
                <th>Enddate</th>
                <th>Prepay</th>
                <th>Fee</th>
                <th>Ispay</th>
                <th>Isprepay</th>
                <th>Attachmentid</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
        {% for project in projects %}
            <tr>
                <td><a href="{{ path('admin_project_show', { 'id': project.id }) }}">{{ project.id }}</a></td>
                <td>{{ project.title }}</td>
                <td>{{ project.type }}</td>
                <td>{{ project.budget }}</td>
                <td>{{ project.description }}</td>
                <td>{% if project.registerDate %}{{ project.registerDate|date('Y-m-d H:i:s') }}{% endif %}</td>
                <td>{% if project.endDate %}{{ project.endDate|date('Y-m-d H:i:s') }}{% endif %}</td>
                <td>{{ project.prePay }}</td>
                <td>{{ project.fee }}</td>
                <td>{% if project.isPay %}Yes{% else %}No{% endif %}</td>
                <td>{% if project.isPrePay %}Yes{% else %}No{% endif %}</td>

                <td>{{ project.attachmentId }}</td>

                <td>{{ project.status }}</td>
                <td>
                    <ul>
                        <li>
                            <a href="{{ path('admin_project_show', { 'id': project.id }) }}">show</a>
                        </li>
                        <li>
                            <a href="{{ path('admin_project_edit', { 'id': project.id }) }}">edit</a>
                        </li>
                    </ul>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>

    <ul>
        <li>
            <a href="{{ path('admin_project_new') }}">Create a new project</a>
        </li>
    </ul>
{% endblock %}

如果我{{ project.fee }}从 twig 模板中删除,我得到了同样的错误,但现在为 attachmentId,如果删除{{ project.attachmentId }}则这些没有错误。

谢谢。

标签: phpsymfony

解决方案


您在实体类中有拼写错误,

public function geFee()

它应该,

public function getFee()

推荐阅读