【问题标题】:Undefined Variable "name" in Enquiry.php (Symfony2)Enquiry.php (Symfony2) 中未定义的变量“名称”
【发布时间】:2013-12-23 22:57:34
【问题描述】:

我一直在关注本教程:tutorial.symblog.co.uk,并且刚刚完成了第 2 部分。我还关注了一位用户在 cmets 中所说的关于教程中不推荐使用的函数的内容,并在我的代码。

但是,当我尝试提交表单时,我仍然收到以下错误:

ContextErrorException:注意:未定义变量:/private_html/symfony/symblog/src/Blogger/BlogBu​​ndle/Entity/Enquiry.php 第 22 行中的名称

我查看了我的 Enquiry.php 文件,但没有找到任何东西。

这里是:

<?php
// src/Blogger/BlogBundle/Entity/Enquiry.php

namespace Blogger\BlogBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;

class Enquiry {
    protected $name;
    protected $email;
    protected $subject;
    protected $body;

    public function getName() {
        return $this->name;
    }

    public function setName() {
        $this->name = $name; // Line 22
    }

    public function getEmail() {
        return $this->email;
    }

    public function setEmail() {
        $this->email = $email;
    }

    public function getSubject() {
        return $this->subject;
    }

    public function setSubject() {
        $this->subject = $subject;
    }

    public function getBody() {
        return $this->body;
    }

    public function setBody() {
        $this->body = $body;
    }

    public static function loadValidatorMetadata(ClassMetadata $metadata) {
        $metadata->addPropertyConstraint('name', new NotBlank())
            ->addPropertyConstraint('email', new Email())
            ->addPropertyConstraint('subject', new NotBlank())
            ->addPropertyConstraint('subject', new Length(array('max'=>50)))
            ->addPropertyConstraint('body', new Length(array('min'=>50)));
    }
}

提前致谢!

【问题讨论】:

    标签: php symfony entity deprecated getter-setter


    【解决方案1】:

    您忘记将参数传递给您的设置器

    public function setName($name) {
        $this->name = $name;
    }
    

    【讨论】:

    • 非常感谢!那真是一个新手的错误……我感到很惭愧。 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多