【问题标题】:Symfony2: Database and Entity settings: Neither property ... nor method ...nor method ... exists in classSymfony2:数据库和实体设置:类中既不存在属性......也不存在方法......也不存在方法......
【发布时间】:2012-10-02 22:53:56
【问题描述】:

我有一个连接到数据库的 Symfony2 项目。对于每个表,我都有一个实体。

现在,我正在尝试使用 ManyToOne 将一个实体与另一个实体连接起来。

问题来了:

我有两个实体:用户和工作场所。

在用户实体中,我有:

 /**
 * @ORM\ManyToOne(targetEntity="Workplace")
 * @ORM\JoinColumn(name="workplace", referencedColumnName="place")
 **/
protected $workplace;

/**
 * Set workplace
 *
 * @param integer $workplace
 */
public function setWorkplace($workplace)
{
    $this->workplace = $workplace;
}

/**
 * Get workplace
 *
 * @return integer 
 */
public function getWorkplace()
{
    return $this->workplace;
}

在我拥有的工作场所实体中:

/**
 * @ORM\Column(type="text")
 */
protected $place;



/**
 * Set place
 *
 * @param text $place
 */
public function setPlace($place)
{
    $this->place = $place;
}

/**
 * Get place
 *
 * @return text 
 */
public function getPlace()
{
    return $this->place;
}

因此,我遇到了一个例外:

Neither property "workplace" nor method "getWorkplace()" nor method "isWorkplace()" exists in class "SciForum\Version2Bundle\Entity\Workplace" 

如何解决。非常感谢。

【问题讨论】:

  • 我认为您错过了每个实体中的 setter 和 getter 函数。
  • 通常不会,我会编辑我的问题并提供更多详细信息。
  • 您能否发送您的控制器代码和工作场所实体?
  • 如何发送这个?发布问题?
  • 哦,我找到了解决方案,在我的控制器中填充表单时,我使用的是 'property' => 'workplace' 而不是 'property' => 'workplace'。

标签: database symfony entity


【解决方案1】:

试试这个

->add('place','entity',  array('class'=>'yourBundle:WorkPlace',
                               'property'=>'place'))

在您的表单中输入。

【讨论】:

    【解决方案2】:

    @Asish AP 是对的,但缺少解释。

    在 formBuilder 中,如果两个实体之间存在关系,则必须在表单类型中指定正确的实体。

    ->add(
        'place',
        'entity',  
            array(
                'class'=>'yourBundle:WorkPlace', //Link your entity
                'property'=>'place' //Specify the property name in the entity
    ))
    

    如果你在formBuilder中指定了一个不存在的属性,就会出现这个错误:

    Neither property "workplace" nor method "getWorkplace()" nor method "isWorkplace()" exists in class "SciForum\Version2Bundle\Entity\Workplace"
    

    这就是你的错误原因和解决方案的解释。

    https://creativcoders.wordpress.com/2014/06/02/sf2-neither-the-property-nor-one-of-the-methods-exist-and-have-public-access-in-class/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 2016-06-14
      • 2018-06-11
      相关资源
      最近更新 更多