Symfony 2, Doctrine 2.1.
I’ve got 3 entities, one of them intermediate (join table). Let’s say it’s SomeObject, SomeProperty and ObjectProperties.
Problem: I can’t get the value of SomeProperty ‘name’ property. Here’s the code:
[...]
class SomeObject
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @var ObjectProperties $objectProperties
*
* @ORM\OneToMany(targetEntity="ObjectProperties", mappedBy="object_id", cascade={"all"})
*/
private $objectProperties;
[...]
[...]
class SomeProperty
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\OneToMany(targetEntity="ObjectProperties", mappedBy="property_id", cascade={"all"})
*/
private $id;
/**
* @var string $name
*
*/
private $name;//I NEED TO GET VALUE OF $name
[...]
[...]
class ObjectProperties
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="SomeObject", inversedBy="id", cascade={"all"})
* @ORM\JoinColumn(name="object_id", referencedColumnName="id")
*/
private $object_id;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="SomeProperty", inversedBy="id", cascade={"all"})
* @ORM\JoinColumn(name="property_id", referencedColumnName="id")
*/
private $property_id;
[...]
Setters and getters as usual.
In my controller I’ve got something along the lines of:
$entity = $em->getRepository('SomeTestBundle:SomeObject')->find($id);
[...]
$props = $entity->getObjectProperties();
foreach ($props as $prop){
echo '---------------------------<br>';
var_dump($prop->getPropertyId()->getName());
}
Now, getName() gives me null result, but if i replace it with getId it works as expected. Same if I try to use it in a form class. Is it because there is no doctrine association between SomeProperty->name and ObjectProperties?
I think I don’t quite get the way doctrine is supposed to work. I thought that only property_id and object_id are needed in a junction table, otherwise it doesn’t make much sense for me, because SomeProperty serves as a dictionary table, so I could change SomeProperty->name in one place.
I’m seriously stuck with it. Is it some sort of configuration option that is lacking or am I generally not getting the bigger picture?
Consider it a typo – I’ve made it work by adding missing @Column annotation in SomeProperty class:
Don’t know why I missed it in the first place… I guess “Experience is something you get, after you need it.”