Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在无symfony项目中重用symfony实体_Symfony - Fatal编程技术网

如何在无symfony项目中重用symfony实体

如何在无symfony项目中重用symfony实体,symfony,Symfony,我在symfony项目中有一些实体,我想在另一个不是用symfony编写的php项目中重用它们。我不能重写这个项目,因为它太大了 是否有办法将我的实体及其职责纳入无符号项目 感谢您的回答如果有人感兴趣,我已经找到了在Symfony之外使用实体和存储库的解决方案 例如,我在/path中安装了一个新的Symfony,并创建了一个简单的实体Testeur <?php // Import require_once $_SERVER['DOCUMENT_ROOT'].'/path/vendor/au

我在symfony项目中有一些实体,我想在另一个不是用symfony编写的php项目中重用它们。我不能重写这个项目,因为它太大了

是否有办法将我的实体及其职责纳入无符号项目


感谢您的回答

如果有人感兴趣,我已经找到了在Symfony之外使用实体和存储库的解决方案

例如,我在/path中安装了一个新的Symfony,并创建了一个简单的实体Testeur

<?php
// Import
require_once $_SERVER['DOCUMENT_ROOT'].'/path/vendor/autoload.php';
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Acme\DemoBundle\Entity\Testeur;


$paths = array("/path/src/Acme/DemoBundle/Entity/");
$isDevMode = true;

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

// Creation of the entity manager
$config = Setup::createConfiguration($isDevMode);
$driver = new AnnotationDriver(new AnnotationReader(), $paths);
AnnotationRegistry::registerLoader('class_exists'); // registering noop annotation autoloader - allow all annotations by default
$config->setMetadataDriverImpl($driver);
$entityManager = EntityManager::create($dbParams, $config);

// Creation of the repository of my Testeur entity
$metadata            = $entityManager->getClassMetadata('Acme\DemoBundle\Entity\Testeur');
$repositoryClassName = $metadata->customRepositoryClassName;
if ($repositoryClassName === null) {
    $configuration       = $entityManager->getConfiguration();
    $repositoryClassName = $configuration->getDefaultRepositoryClassName();
}
/** @var \Acme\DemoBundle\Entity\TesteurRepository $tR */
$repo =  new $repositoryClassName($entityManager, $metadata);

// Example insertion
$obj = new Testeur();
$obj->setLib('First object');
$obj->setDescription('This is my first object');
$obj->setSort(1);
$obj->setVisible(true);
$entityManager->persist($obj);
$entityManager->flush();

// Get an objet by the repo
$o = $repo->find(17);
echo "<pre>".print_r($o, true)."</pre>";  

现在,有了这个解决方案,我可以将我的实体应用到一个巨大的垃圾项目中,而这个项目不是用symfony制作的

您的其他项目是否使用了条令?这取决于您的存储库代码如何依赖于symfony组件。如果您没有向存储库中注入任何服务,那么您可以在Symfony之外安全地使用您的实体。你只需要使用ORM,即条令。