Reputation: 20223
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
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.
Upvotes: 5
Reputation: 4441
Try this
->add('place','entity', array('class'=>'yourBundle:WorkPlace',
'property'=>'place'))
in your form Type.
Upvotes: 6