2012-01-20 55 views
0

實體symfony的2另一個項目中使用的實體從Symfony2中有前面有@ORM \主義標籤(例如@ORM \實體)。我怎麼能只使用doctrine2

當使用教義作爲組分,預計標籤而不ORM(例如@Entity)。

如果我想在使用的學說,但沒有symfony的這會導致問題的另一個項目中使用這些實體從symfony項目。

我怎麼可以設置非symfony項目中使用這些類與@ORM \標籤?

+0

你打算使用比symfony的不同一個框架?如果是的話,哪一個? – jere

+0

是的......但它是一個傳統的定製框架。 – YakobeYak

+0

嗯..我覺得只要你有自動加載類的方式,你可以讓它工作...在PHP中使用新的命名空間,你可以把教義庫在你的框架的「LIB」文件夾,自動加載它,然後從你的實體,你可以做一些像'使用\ YourLibs \ Doctrine \ ORM作爲ORM',所以你不必在你的實體中改變任何事......雖然不太確定... – jere

回答

0

目前我已經爲我的意見周圍由MadManMonty建議的工作問題:

你有使用不同的映射方法,例如考慮作爲yaml或xml來管理而不是註釋,因爲它可能提供了一種簡單的替代方法來解決該問題? - MadManMonty

0

@ORM\前綴不Symfony的具體。它在Doctrine通用文檔中:http://www.doctrine-project.org/docs/common/2.2/en/reference/annotations.html#introduction

您必須按文檔中所述註冊您使用AnnotationRegistry::registerFile()的註釋文件。另外,檢查Symfony的app/autoload.php文件。它註冊了自動加載磁帶機:在AnnotationRegistry

AnnotationRegistry::registerLoader(function($class) use ($loader) { 
    $loader->loadClass($class); 
    return class_exists($class, false); 
}); 
AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); 
0

我得到它的工作像這樣通過EntityManager的$ useSimpleAnnotationReader設置爲false ::創建():

// bootstrap.php 
require_once "vendor/autoload.php"; 

use Doctrine\ORM\Tools\Setup; 
use Doctrine\ORM\EntityManager; 
use Doctrine\Common\Annotations\AnnotationRegistry; 

//IGNORE IF YOU DONT NEED SERIALIZER/VALIDATOR 
AnnotationRegistry::registerAutoloadNamespace('JMS\Serializer\Annotation', $vendorPath . '/jms/serializer/src'); 
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\Validator\Constraint', $vendorPath . '/symfony/validator'); 
//IGNORE IF YOU DONT NEED SERIALIZER/VALIDATOR 

AnnotationRegistry::registerFile($vendorPath . '/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); 

$paths = array("/path/to/entities-or-mapping-files"); 
$isDevMode = false; 

// the connection configuration 
$dbParams = array(
    'driver' => 'pdo_mysql', 
    'user'  => 'root', 
    'password' => '', 
    'dbname' => 'foo', 
); 

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); 
//LAST PARAMETER SETS $useSimpleAnnotationReader TO FALSE 
$entityManager = EntityManager::create($dbParams, $config, null, null, false);