Symfony2-Doctrine2继承仍然无法正常工作 [英] Symfony2-Doctrine2 inheritance persist not working properly

查看:93
本文介绍了Symfony2-Doctrine2继承仍然无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Symfony 2.3中有一个使用Doctrine ORM 2.3.4的项目,并且正在使用类继承:

I have a project in Symfony 2.3, using Doctrine ORM 2.3.4, and I'm using class inheritance:

父类

/**
 * @ORM\Entity
 * @ORM\Table(name="parent")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"child"="Child"})
 */
class Parent
{
   /**
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   private $id;

   public function getId()
   {
      return $this->id;
   }

   // other fields & methods
}

和一个孩子

/**
 * @ORM\Entity
 * @ORM\Table(name="child")
 */
class Child extends Parent
{
   /**
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    */
   private $id;

   public function getId()
   {
      return $this->id;
   }
}

问题是当我坚持子对象冲洗时出现的然后我尝试检索子ID:

The problem comes when I persist the child object, flush and then I try to retrieve the child id:

// ChildController::createAction

$em = $this->getDoctrine()->getManager();
$child = new Child();
// set child fields
$em->persist($child);
$em->flush();

$child->getId(); // <- not working

在数据库上,子行已正确保存,如果我更改子方法 getId

On the database the child row is saved correctly, and if I change the child method getId

public function getId()
{
   return parent::getId();
}

它起作用。

有人可以向我解释吗?

非常感谢。

推荐答案

父实体需要向其子级提供其属性的可见性。

The parent entity needs to give visibility of it's properties to it's children.

将您的$ id属性可见性更改为受保护。

Change your $id property visibility to "protected".

这篇关于Symfony2-Doctrine2继承仍然无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆