Symfony2 the table prefix tutorial with entity new database

Source: Internet
Author: User
Tags php file

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
phpMyAdmin View Database
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.