在symfony2学说中延迟加载

时间:2014-12-15 16:07:59

标签: php symfony doctrine-orm

我在Symfony2框架中使用Doctrine(v 2.3.23)。我有两个实体:ProductManufacturer,单向关系一对多。实体具有小写字段名称。当我从存储库中检索数据并尝试使用以下代码访问延迟加载的对象时:

$repository = $this->getDoctrine ()->getRepository ( 'AtCompDbBundle:Product' );
$result = $repository->findById ( 1);
$x = $result[0]->getManufacturer ()->getName ();

我收到以下错误:

ContextErrorException: Notice: Undefined property: 
Proxies\__CG__\AtComp\DbBundle\Entity\Manufacturer::$Name in C:\project_dir\src\AtComp\DbBundle\Entity\Manufacturer.php line 54

我找到了一个有效的解决方法:将字段名称更改为以大写字母开头。但有没有办法强制学说代理对象查找以小写字母开头的字段名称?

以下是Product类定义:

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

制造商类的定义:

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

1 个答案:

答案 0 :(得分:3)

属性的名称应为小写,对set方法执行相同的操作:

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}