dongliuxia9495 2013-12-23 22:57
浏览 59
已采纳

Enquiry.php中未定义的变量“name”(Symfony2)

I have been following this tutorial: tutorial.symblog.co.uk, and just finished up Part 2. I also followed what one of the users said in the comments about the deprecated functions that were part of the tutorial, and I fixed those in my code.

However, I still get the following error when I try to submit my form:

ContextErrorException: Notice: Undefined variable: name in /private_html/symfony/symblog/src/Blogger/BlogBundle/Entity/Enquiry.php line 22

I've looked through my Enquiry.php file, but haven't found anything.

Here it is:

<?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)));
    }
}

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dqq9695 2013-12-23 23:02
    关注

    You forgot to pass the parameter into your setter

    public function setName($name) {
        $this->name = $name;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?