I use Doctrine in Symfony2 framework (v 2.3.23). I have two entities: Product and Manufacturer with one-direction relation one-to-many. Entities have lowercase field names. When I retrieve data from repository and try to access lazy loaded object using this code:
$repository = $this->getDoctrine ()->getRepository ( 'AtCompDbBundle:Product' );
$result = $repository->findById ( 1);
$x = $result[0]->getManufacturer ()->getName ();
I get following error:
ContextErrorException: Notice: Undefined property:
Proxies\__CG__\AtComp\DbBundle\Entity\Manufacturer::$Name in C:\project_dir\src\AtComp\DbBundle\Entity\Manufacturer.php line 54
I found a workaround that works: change the field names to begin with capital letter. But is there any way to force doctrine proxy objects to look for field names starting with lowercase letter?
Here is the Product class definition:
/**
* @ORM\Entity(repositoryClass="AtComp\DbBundle\Repository\ProductRepository")
* @ORM\Table(name="produkty")
*/
class Product {
/**
* @ORM\Column(type="integer", name="prod_id")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Manufacturer")
* @ORM\JoinColumn(name="prod_producent_id", referencedColumnName="pr_id")
*/
protected $manufacturer;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set manufacturer
*
* @param \AtComp\DbBundle\Entity\Manufacturer $manufacturer
* @return Product
*/
public function setManufacturer(\AtComp\DbBundle\Entity\Manufacturer $manufacturer = null) {
$this->manufacturer = $manufacturer;
return $this;
}
/**
* Get manufacturer
*
* @return \AtComp\DbBundle\Entity\Manufacturer
*/
public function getManufacturer() {
return $this->manufacturer;
}
}
And the definition of Manufacturer class:
/**
* @ORM\Entity
* @ORM\Table(name="producent")
*/
class Manufacturer
{
/**
* @ORM\Column(type="integer", name="pr_id")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// CHANGE OF THE DEFINITION OF THIS FIELD INTO
//
// protected $Name;
//
// SOLVES THE PROBLEM...
/**
* @ORM\Column(type="string", length=100, name="pr_name")
*/
protected $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->Id;
}
/**
* Set name
*
* @param string $name
* @return Manufacturer
*/
public function setName($name)
{
$this->Name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->Name;
}
}