Symfony2:数据库和实体设置:类中既不存在属性...也不存在方法......也不存在方法....

Mil*_*loš 4 database entity symfony

我有一个连接到数据库的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;
}
Run Code Online (Sandbox Code Playgroud)

在Workplace Entity我有:

/**
 * @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;
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我得到一个例外:

Neither property "workplace" nor method "getWorkplace()" nor method "isWorkplace()" exists in class "SciForum\Version2Bundle\Entity\Workplace" 
Run Code Online (Sandbox Code Playgroud)

怎么能解决这个问题.非常感谢你.

Asi*_* AP 6

试试这个

->add('place','entity',  array('class'=>'yourBundle:WorkPlace',
                               'property'=>'place'))
Run Code Online (Sandbox Code Playgroud)

在您的表单中键入.


Edo*_*mbo 5

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

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

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

如果在formBuilder中指定了一个不存在的属性,则会出现以下错误:

Neither property "workplace" nor method "getWorkplace()" nor method "isWorkplace()" exists in class "SciForum\Version2Bundle\Entity\Workplace"
Run Code Online (Sandbox Code Playgroud)

这就是您的错误和解决方案的解释的原因.

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