一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Symfony2用Entity新建数据库加入表前缀教程

时间:2016-06-01 编辑:简简单单 来源:一聚教程网

直接上代码:

假设我们在组织Acme下新建了DemoBundle了,比如src\Acme\DemoBundle

1.新建Entity

php app/console --shell
generate:doctrine:entity --entity=AcmeDemoBundle:Admin
// 或者
php app/console generate:doctrine:entity --entity=AcmeDemoBundle:Admin

2.设置数据库字段类型等

2.1在控制台用指令生成Entity

屏幕截图 2015年4月3日 at 上午12.00.56

屏幕截图 2015年4月3日 at 上午12.01.25
2.2 查看目录文件和数据库
屏幕截图 2015年4月3日 at 上午12.03.59
屏幕截图 2015年4月3日 at 上午12.05.42
2.3把表的名称设置一下
屏幕截图 2015年4月3日 at 上午12.52.47
2.4生成数据库
1
doctrine:schema:update --force
phpMyAdmin查看数据库
屏幕截图 2015年4月3日 at 上午12.15.26屏幕截图 2015年4月3日 at 上午12.14.12
3. 启用服务设置表前缀zm_
3.1 在services.yml中设置服务参数
屏幕截图 2015年4月3日 at 上午12.30.28

 

 代码如下 复制代码
# parameters
parameters:
    AcmeDemoBundle.db.table_prefix: zm_
# services
services:
    AcmeDemoBundle.tblprefix_subscriber:
            class: Acme\DemoBundle\Subscriber\TablePrefixSubscriber
            arguments: [%AcmeDemoBundle.db.table_prefix%]
            tags:
                - { name: doctrine.event_subscriber }

3.2 创建文件
屏幕截图 2015年4月3日 at 上午12.37.58

如上图所示新建 TablePrefixSubscriber.php 文件内容如下

 

 代码如下 复制代码

namespace Acme\DemoBundle\Subscriber;
 
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use \Doctrine\Common\EventSubscriber;
 
class TablePrefixSubscriber implements EventSubscriber
{
    protected $prefix = '';
 
    public function __construct($prefix)
    {
        $this->prefix = (string) $prefix;
    }
 
    public function getSubscribedEvents()
    {
        return array('loadClassMetadata');
    }
 
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
    {
        $classMetadata = $args->getClassMetadata();
        if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
            // if we are in an inheritance hierarchy, only apply this once
            return;
        }
        // Only add the prefixes to our own entities.
        if (false === strpos($classMetadata->namespace, 'Acme\DemoBundle')) {
            return;
        }
        // Do not re-apply the prefix when the table is already prefixed
        if (false !== strpos($classMetadata->getTableName(), $this->prefix)) {
            return;
        }
 
        $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
 
        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
            if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) {
                $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
                // Do not re-apply the prefix when the association is already prefixed
                if (false !== strpos($mappedTableName, $this->prefix)) {
                    continue;
                }
                $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
            }
        }
    }
 
}
3.3生成数据库

doctrine:schema:update --force

OK,Symfony2设置表前缀结束了,Please enjoy it!

热门栏目