Doctrine - Managed Entity - 默认情况下不管理获取的实体

Mon*_*roM 5 php typo3 unit-of-work symfony doctrine-orm

我将 Doctrine 用于 Typ3/cms 项目以增强后端工作流程的功能。

所以我不得不自己引导教义。它的大部分内容都非常简单,我完全没有问题。但是当谈到坚持一个现有的实体时,我很挣扎。每次我坚持一个现有实体时,它都会被创建为一个新实体。

经过一番挖掘,我得出结论,这不是“UnitOfWork”的一部分(->contains(entity) == false)。如果我在这个单元内手动注册,一切又正常了。

$this->entityManager->getUnitOfWork()->registerManaged($page, array('uid' => $page->getUid()), array('title' => $page->getTitle()));
Run Code Online (Sandbox Code Playgroud)

但这不可能是故事的结局..所以我仍然试图弄清楚我的学说有什么问题:D

为什么我提取的实体不受管理?

这是我的 DoctrineLoader:

private function createEntityManager()
{
    global $GLOBALS;

    $paths = array(
        MyT3Extension::rootDir() . '/Configuration/ORM'
    );
    $isDevMode = true;
    $typoDbConfig = $GLOBALS['TYPO3_CONF_VARS']['DB'];

    // the connection configuration
    $dbParams = array(
        'driver'   => 'pdo_mysql',
        'user'     => $typoDbConfig['username'],
        'password' => $typoDbConfig['password'],
        'dbname'   => $typoDbConfig['database'],
        'charset'  => 'utf8'
    );

    $config = Setup::createYAMLMetadataConfiguration($paths, $isDevMode, MyT3Extension::rootDir() . '/Cache');
    $entityManager = EntityManager::create($dbParams, $config);


    return $entityManager;
}
Run Code Online (Sandbox Code Playgroud)

orm 模型的 Yml 定义:

Vendor\TypoBundle\Entity\Page:
    type:  entity
    table: pages
    id: { uid: { type: integer, generator: { strategy: AUTO } } }
    fields:
        pid: { type: integer }
        title: { type: string }
        navTitle: { type: string, column: nav_title }
        doctype: { type: integer, column: doktype }
        isSiteroot: { type: boolean, column: is_siteroot }
        layout: { type: integer }
Run Code Online (Sandbox Code Playgroud)

用法示例代码是:

$page = $this->entityManager->getRepository('Vendor\TypoBundle\Entity\Page')->findOneBy(array());
$page->setTitle('Test');
$this->entityManager->persist($page);
$this->entityManager->flush(); // will create a new record (new uid)
Run Code Online (Sandbox Code Playgroud)

什么工作是这样的:

$page = $this->entityManager->getRepository('Vendor\TypoBundle\Entity\Page')->findOneBy(array());

$this->entityManager->getUnitOfWork()->registerManaged(
     $page, 
     array(
         'uid' => $page->getUid()
     ), array(
         'title' => $page->getTitle()
     )
);

$page->setTitle('Test');
$this->entityManager->persist($page);
$this->entityManager->flush();
Run Code Online (Sandbox Code Playgroud)

所以..我希望任何人都可以帮助我:D(我会在教义symfony包中寻找解决方案..)

Mon*_*roM 4

好的。我找到了解决方案。我在typo3 extbase中使用了特殊的依赖注入“技术”。如果您将依赖项指定为 __construct() 的参数,它将注入服务而无需任何进一步的配置。所以我只是假设,它使用内部“服务总线”来为具有相同对象实例的所有依赖项提供服务,但它为每个依赖项请求创建一个。

所以我有三个不同的实体经理,显然不知何故没有管理实体..很抱歉打扰你们。