Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine - Managed Entity - Fetched Entites are not managed by default

I am using Doctrine for a typo3/cms project to enhance the power of the backend workflows.

So i had to boot doctrine all on my own. The most part of it was quite easy and i hat no problems at all. But when it came to persisting an existing entity i struggeld. Everytime i persisted an existing entity, it was created as a new one.

After some digging i came to the conclusion, that is not part of the "UnitOfWork" (->contains(entity) == false). If i registered it manually within this unit everything worked fine again.

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

But this can't be the end of the story.. so i still try to figure out what when wrong with my doctrine :D

Why are my fetched entites not 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 }

Example code for usuage is:

$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 is sth 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 anyone can help me :D (i will look in the doctrine symfony bundle for a solution..)

like image 920
MonocroM Avatar asked Sep 28 '22 22:09

MonocroM


1 Answers

Ok. I found the solution. I used a special dependency injection "technique" in typo3 extbase. If you specify your dependencies in as arguments of __construct() it will inject the service without any further configuration. So i just assumed, that it uses an internal 'service bus' to serve all dependencies with the same object instances, but instead it created one for each dependency request.

So i got three diffrent entity managers and obviously somehow not managed entities.. sorry to bothered you folks.

like image 50
MonocroM Avatar answered Oct 03 '22 02:10

MonocroM