首页 文章

Doctrine2错误在生成实体时从MappedSuperClass导入'private'变量

提问于
浏览
2

我将实体类与Doctrine的ORM注释结合起来,将值保存到数据库中 . 大多数表需要一些标准字段,因此我创建了一个所有其他实体都可以扩展的基本实体 . 根据文档,这称为MappedSuperClass:http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

// src/Acme/Bundle/Entity/Base.php
namespace Acme\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\MappedSuperclass
 * @ORM\HasLifecycleCallbacks()
*/
class Base {

    /**
    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    // more values...

}

然后我创建了多个扩展此基础的实体:

// src/Acme/Bundle/Entity/View.php
namespace Acme\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;

View extends Entity\Base
{
     // entity definitions
}

Is this the best way to set default definitions for an entity? If you have better suggestions, let me know.

接下来,当我通过以下方式生成实体:php app / console doctrine:generate:entities Acme \ Bundle

第一次创建getter和setter时效果很好,但如果我对实体进行更改并再次生成,则会出现如下错误:

Fatal error: Access level to Acme\Bundle\Entity\View::$id must be protected (as in class Acme\Bundle\Entity\Base) or weaker in /src/Acme/Bundle/Entity/View.php

发生这种情况是因为 doctrine:generate:entities 正在将'protected'变量从MappedSuperClass导入扩展实体'private' .

其他人在没有解决方案的情况下在其他情况下抱怨此错误:FOSUserBundle generate:entities does not work, Access level of fields too high https://github.com/FriendsOfSymfony/FOSUserBundle/issues/102

问题:学说是否应该导入受保护的变量?是应该将它们设置为“私人”?或者这只是一个已知的Symfony错误?

看起来它不应该导入受保护的变量,因为@ORM定义已经在MappedSuperClass中并且没有导入(并且当我删除导入的私有变量时它工作正常) . 但如果确实导入了它们,则不应将它们设置为私有...

我实际上必须进行搜索并通过我的所有实体替换它们以删除它们 . 每一个 . 单 . 时间 .

What is the suggested course of action here? If this is a bug, has someone reported this and what is the timeline for fixing? How should I search for this issue on github and report it if it's not reported?

问题2:只要存在此错误,是否有办法在单个实体上生成getter / setter?例如 php app/console doctrine:generate:entities Acme\Bundle\Entity\View (当然这不起作用) . 如果我一次只能生成一个实体,那么删除所有实体中所有导入的私有变量就不那么麻烦了 . [编辑:我在下面回答了这个问题]

2 回答

  • 0

    #2的答案是,是的,你可以只为一个实体生成实体getter / setter .

    如果键入命令行,请从命令行输入:

    php app/console doctrine:generate:entities --help
    

    你得到一个选项列表 . 在那里,您将看到如何将实体限制为捆绑,捆绑中的单个实体或整个命名空间:

    You have to limit generation of entities:
    * To a bundle: 
    php app/console doctrine:generate:entities MyCustomBundle
    
    * To a single entity:
    php app/console doctrine:generate:entities MyCustomBundle:User
    php app/console doctrine:generate:entities MyCustomBundle/Entity/User
    
    * To a namespace
    php app/console doctrine:generate:entities MyCustomBundle/Entity
    
  • 0

    对于问题1,您需要在继承实体时重新定义每个主键 .

相关问题