Doctrine - Managed Organization - Injected Entites are not managed by default
I am using Doctrine for a typo3 / cms project to make my workflows more efficient.
So I had to teach the doctrine on my own. Most of it was pretty simple and I had no problems at all. But when it came to preserving the existing entity, I struggled. Every time I saved an existing object, it was created as a new one.
After some digging I came to the conclusion that this is not part of "UnitOfWork" (-> contains (entity) == false). If I registered it manually on this device everything worked fine again.
$this->entityManager->getUnitOfWork()->registerManaged($page, array('uid' => $page->getUid()), array('title' => $page->getTitle()));
But that can't be the end of the story .. so I'm still trying to figure out what if wrong with my doctrine: D
Why are my entites received not being managed?
This is my 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;
}
Yml definition for orm model:
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 }
Sample code to use:
$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)
What works like:
$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();
So, I hope someone can help me: D (I'll take a look at the symfony bundle teachings for a solution ..)
source to share
Ok .. I found a solution .. I used a special injection "injection" in typo3 extbase. If you specify your dependencies as arguments to __concstruct (), it will inject the service without any additional configuration. So I just assumed it was using an internal "Service Bus" to serve all dependencies with the same object instances, but instead it was creating one for each dependency request.
So I got three different entity managers .. and obviously didn't manage the objects somehow .. sorry to bother you guys.
source to share