在Doctrine2中生成带前缀的实体

时间:2011-01-06 08:22:41

标签: doctrine

我在Zend Framework中使用doctrine2。构建实体时需要设置类的前缀,例如(Model User)。可能吗?。我使用的命令是

./doctrine orm:generate-entities --generate-annotations=1 ../../../application/models/

2 个答案:

答案 0 :(得分:1)

我是Doctrine orm的新手,但我做到了这一点:

   class TablePrefix{

    protected $prefix = '';

    public function __construct($prefix){
        $this->prefix = '';

        for($i = 0; $i < strlen($prefix); $i++){ // table prefix like in entity name
            if( $prefix[$i] == '_' && $i-1 > 0 ){
                $prefix[$i-1] = strtoupper($prefix[$i-1]);
            }
            else{
                $this->prefix .= $prefix[$i];
            }
        }

        $this->prefix[0] = strtoupper($this->prefix[0]);
    }

    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs){

        $classMetadata = $eventArgs->getClassMetadata();

        $tmp = substr($classMetadata->name, 0, strlen($this->prefix));

        if($tmp == $this->prefix ){
            $classMetadata->name = substr($classMetadata->name, strlen($this->prefix));
        }
    }
}

接下来当您创建实体管理器时,请执行以下操作:

$tablePrefix = new TablePrefix('tbl_');
    $evm = $entityManager->getEventManager();
    $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefix);

在文件顶部添加:use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;

当你运行cmd php vendor / bin / doctrine orm时:generate-entities模型模型的名称就可以了!

答案 1 :(得分:-1)

在application.ini文件中添加这些行。

doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = false
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "ModuleName_Model_"