How to setup doctrine table prefix in symfony2
首先创建一个 bundle,
1 |
php app/console generate:bundle --namespace=Vshop/Bundle/CoreBundle --format=xml |
然后打开 CoreBundle 里面的这个 services.xml 文件
1 |
src/Vshop/Bundle/CoreBundle/Resources/config/services.xml |
我的项目配置文件进行了分割,方便以后的管理,目录结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
然后编写事件订阅类:
1 |
src/Vshop/Bundle/CoreBundle/Event/Doctrine/TablePrefixSubscriber |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
/** * This file is part of the themetouch vshop package. * * (c) zhou * * For the full copyright and license information, * please view the LICENSE file that was distributed with this source code. * * @author zhou * @version 0.1.0 * @copyright Copyright (c) 2014-2015, themetouch.com * @link http://www.themetouch.com/ */ namespace Vshop\Bundle\CoreBundle\Event\Doctrine; use Doctrine\Common\EventSubscriber; use Doctrine\ORM\Event\LoadClassMetadataEventArgs; use Doctrine\ORM\Mapping\ClassMetadataInfo; class TablePrefixSubscriber implements EventSubscriber { protected $prefix = ''; public function __construct($prefix = null) { $this->setPrefix($prefix); } /** * Set the prefix * * @param string $prefix * * @return TablePrefixSubscriber */ public function setPrefix($prefix = null) { $this->prefix = (string) $prefix; return $this; } /** * Get the prefix * * @return the $prefix */ public function getPrefix() { return $this->prefix; } public function getSubscribedEvents() { return array( 'loadClassMetadata' ); } public function loadClassMetadata(LoadClassMetadataEventArgs $args) { $classMetadata = $args->getClassMetadata(); if ($classMetadata->isInheritanceTypeSingleTable() && ! $classMetadata->isRootEntity()) { return; } $classMetadata->setTableName($this->prefix . $classMetadata->getTableName()); foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY) { $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName; } } } } |
然后实体类里面还是只需要写不带表前缀的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
/** * This file is part of the themetouch vshop package. * * (c) zhou * * For the full copyright and license information, * please view the LICENSE file that was distributed with this source code. * * @author zhou * @version 0.1.0 * @copyright Copyright (c) 2014-2015, themetouch.com * @link http://www.themetouch.com/ */ namespace Vshop\Bundle\CoreBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * User * * @ORM\Table(name="users") # 注意这里只需要写不带表前缀的表明 * @ORM\Entity(repositoryClass="Vshop\Bundle\CoreBundle\Repository\UserRepository") */ class User { /** * @var integer * * @ORM\Column(name="id", type="bigint") * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @return the $id */ public function getId() { return $this->id; } /** * @param number $id */ public function setId($id) { $this->id = $id; return $this; } } |
表结构如下,然后就ok啦...:-)

No Comments