Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Symfony2: Database and Entity settings: Neither property ... nor method ...nor method ... exists in class

I have an Symfony2 project cnnected to the database. For each table I have an entity.

Now, I am trying to connect one Entity with another using ManyToOne.

Here is the problem:

I have Two entitys: User and Workplace.

In the User Entity, I have:

 /**
 * @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;
}

In the Workplace Entity I have:

/**
 * @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;
}

And with that, I am getting an exception:

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

How could this be resolved. Thank you very much.

Upvotes: 4

Views: 10426

Answers (2)

Edouard Kombo
Edouard Kombo

Reputation: 503

@Asish AP is right, but an explanation is missing.

In the formBuilder, and if you have a relation between two entities, you have to specify the correct entity in your form type.

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

If you specify in the formBuilder, a property that doesn't exist, you will have this error:

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

That was the reason of your error and the explanation of the solution.

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

Upvotes: 5

Asish AP
Asish AP

Reputation: 4441

Try this

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

in your form Type.

Upvotes: 6

Related Questions