Directly on the code:
Let's say we have a new demobundle under the organization ACME, like Src\acme\demobundle.
1. New entity
PHP app/console--shell
Generate:doctrine:entity--entity=acmedemobundle:admin
Or
PHP app/console generate:doctrine:entity--entity=acmedemobundle:admin
2. Set database field types, etc.
2.1 Generating entity in the console with instructions
2.2 View directory files and databases
2.3 Set the name of the table
2.4 Building the database
1
|
doctrine:schema:update --force
|
3. Enable Service Settings table prefix zm_
3.1 Setting service parameters in Services.yml
The code is as follows |
Copy Code |
# 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 Creating files
As shown in the figure above, the new tableprefixsubscriber.php file contains the following
The code is as follows |
Copy Code |
<?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 a inheritance hierarchy, only apply this once Return } Add the prefixes to our own entities. if (false = = = Strpos ($classMetadata->namespace, ' Acme\demobundle ')) { Return } Do isn't 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 isn't 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 Building the database Doctrine:schema:update--force Ok,symfony2 Set table prefix end, please enjoy it! |