开发者

Generate entities with prefix in Doctrine2

开发者 https://www.devze.com 2023-02-02 13:07 出处:网络
I\'m using doctrine2 with Zend Framework. When you build the entites need to set the prefix of the cla开发者_开发技巧sses, for example (Model_User). Is it possible?. The command I use is

I'm using doctrine2 with Zend Framework. When you build the entites need to set the prefix of the cla开发者_开发技巧sses, for example (Model_User). Is it possible?. The command I use is

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


I'm new to Doctrine orm but I made this:

   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));
        }
    }
}

next when you create entity manager make this:

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

on top of file add:use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;

when you run cmd php vendor/bin/doctrine orm:generate-entities models model's name will be ok!


add these lines in your application.ini file.

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_"
0

精彩评论

暂无评论...
验证码 换一张
取 消