Unexpected behavior of doctrine entity manager












0















I have a helper service that links me to a database on the fly.



$databaseName = 'db_x'; 

$em = $this->entityManagerProvider->get($databaseName);


I can use this entity manager like this:



$car = new Car();
$car->setName('Audi');

$em->persist($car);
$em->flush();


It works good! Object was inserted to my db_x



But If I run repository method on this entity manager:



$em->getRepository('AppEntityCar')->findById(1);


Then entity manager try run on my default database, not db_x. How it is possible?



My service to switch database:



namespace AppUtilsDatabase;

use DoctrineORMEntityManager;
use DoctrineORMEntityManagerInterface;

class EntityManagerProvider
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param $databaseName
* @return EntityManagerInterface
* @throws DoctrineORMORMException
*/
public function get($databaseName): EntityManagerInterface
{
return EntityManager::create(
$this->configureParams($databaseName),
$this->entityManager->getConfiguration(),
$this->entityManager->getEventManager()
);
}

/**
* @param string $databaseName
* @return array
*/
private function configureParams(string $databaseName): array
{
$params = $this->entityManager->getConnection()->getParams();

unset(
$params['dbname'],
$params['path'],
$params['url'],
$params["wrapperClass"]
);

$params['dbname'] = $databaseName;

return $params;
}
}









share|improve this question




















  • 1





    You are sure it is the exact same $em instance? Does your repository extend from ServiceEntityRepository?

    – Cerad
    Nov 21 '18 at 20:16













  • @Cerad, yes extend ServiceEntityRepository...

    – Jarek Kowol
    Nov 21 '18 at 20:19






  • 1





    A ServiceEntityRepository find the first entity manager which supports your entity. That seems to be what is happening though I would have thought that $em->getRepo would always use $em. Just for kicks, try extending the repository from EntityRepository and see if that makes any difference. Just a guess.

    – Cerad
    Nov 21 '18 at 20:22











  • And while unrelated, using the same event manager for different entity managers might also cause some amusing problems. Never tried it. I would probably avoid making my EntityManagerProvider dependent on the default entity manager. I see what you are trying to do. Just not sure if it will actually work or not.

    – Cerad
    Nov 21 '18 at 20:34











  • I should think about another solution, but extending from EntityRepository looks work! Thank you.

    – Jarek Kowol
    Nov 21 '18 at 20:56
















0















I have a helper service that links me to a database on the fly.



$databaseName = 'db_x'; 

$em = $this->entityManagerProvider->get($databaseName);


I can use this entity manager like this:



$car = new Car();
$car->setName('Audi');

$em->persist($car);
$em->flush();


It works good! Object was inserted to my db_x



But If I run repository method on this entity manager:



$em->getRepository('AppEntityCar')->findById(1);


Then entity manager try run on my default database, not db_x. How it is possible?



My service to switch database:



namespace AppUtilsDatabase;

use DoctrineORMEntityManager;
use DoctrineORMEntityManagerInterface;

class EntityManagerProvider
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param $databaseName
* @return EntityManagerInterface
* @throws DoctrineORMORMException
*/
public function get($databaseName): EntityManagerInterface
{
return EntityManager::create(
$this->configureParams($databaseName),
$this->entityManager->getConfiguration(),
$this->entityManager->getEventManager()
);
}

/**
* @param string $databaseName
* @return array
*/
private function configureParams(string $databaseName): array
{
$params = $this->entityManager->getConnection()->getParams();

unset(
$params['dbname'],
$params['path'],
$params['url'],
$params["wrapperClass"]
);

$params['dbname'] = $databaseName;

return $params;
}
}









share|improve this question




















  • 1





    You are sure it is the exact same $em instance? Does your repository extend from ServiceEntityRepository?

    – Cerad
    Nov 21 '18 at 20:16













  • @Cerad, yes extend ServiceEntityRepository...

    – Jarek Kowol
    Nov 21 '18 at 20:19






  • 1





    A ServiceEntityRepository find the first entity manager which supports your entity. That seems to be what is happening though I would have thought that $em->getRepo would always use $em. Just for kicks, try extending the repository from EntityRepository and see if that makes any difference. Just a guess.

    – Cerad
    Nov 21 '18 at 20:22











  • And while unrelated, using the same event manager for different entity managers might also cause some amusing problems. Never tried it. I would probably avoid making my EntityManagerProvider dependent on the default entity manager. I see what you are trying to do. Just not sure if it will actually work or not.

    – Cerad
    Nov 21 '18 at 20:34











  • I should think about another solution, but extending from EntityRepository looks work! Thank you.

    – Jarek Kowol
    Nov 21 '18 at 20:56














0












0








0








I have a helper service that links me to a database on the fly.



$databaseName = 'db_x'; 

$em = $this->entityManagerProvider->get($databaseName);


I can use this entity manager like this:



$car = new Car();
$car->setName('Audi');

$em->persist($car);
$em->flush();


It works good! Object was inserted to my db_x



But If I run repository method on this entity manager:



$em->getRepository('AppEntityCar')->findById(1);


Then entity manager try run on my default database, not db_x. How it is possible?



My service to switch database:



namespace AppUtilsDatabase;

use DoctrineORMEntityManager;
use DoctrineORMEntityManagerInterface;

class EntityManagerProvider
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param $databaseName
* @return EntityManagerInterface
* @throws DoctrineORMORMException
*/
public function get($databaseName): EntityManagerInterface
{
return EntityManager::create(
$this->configureParams($databaseName),
$this->entityManager->getConfiguration(),
$this->entityManager->getEventManager()
);
}

/**
* @param string $databaseName
* @return array
*/
private function configureParams(string $databaseName): array
{
$params = $this->entityManager->getConnection()->getParams();

unset(
$params['dbname'],
$params['path'],
$params['url'],
$params["wrapperClass"]
);

$params['dbname'] = $databaseName;

return $params;
}
}









share|improve this question
















I have a helper service that links me to a database on the fly.



$databaseName = 'db_x'; 

$em = $this->entityManagerProvider->get($databaseName);


I can use this entity manager like this:



$car = new Car();
$car->setName('Audi');

$em->persist($car);
$em->flush();


It works good! Object was inserted to my db_x



But If I run repository method on this entity manager:



$em->getRepository('AppEntityCar')->findById(1);


Then entity manager try run on my default database, not db_x. How it is possible?



My service to switch database:



namespace AppUtilsDatabase;

use DoctrineORMEntityManager;
use DoctrineORMEntityManagerInterface;

class EntityManagerProvider
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param $databaseName
* @return EntityManagerInterface
* @throws DoctrineORMORMException
*/
public function get($databaseName): EntityManagerInterface
{
return EntityManager::create(
$this->configureParams($databaseName),
$this->entityManager->getConfiguration(),
$this->entityManager->getEventManager()
);
}

/**
* @param string $databaseName
* @return array
*/
private function configureParams(string $databaseName): array
{
$params = $this->entityManager->getConnection()->getParams();

unset(
$params['dbname'],
$params['path'],
$params['url'],
$params["wrapperClass"]
);

$params['dbname'] = $databaseName;

return $params;
}
}






symfony doctrine






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:16







Jarek Kowol

















asked Nov 21 '18 at 20:02









Jarek KowolJarek Kowol

3331515




3331515








  • 1





    You are sure it is the exact same $em instance? Does your repository extend from ServiceEntityRepository?

    – Cerad
    Nov 21 '18 at 20:16













  • @Cerad, yes extend ServiceEntityRepository...

    – Jarek Kowol
    Nov 21 '18 at 20:19






  • 1





    A ServiceEntityRepository find the first entity manager which supports your entity. That seems to be what is happening though I would have thought that $em->getRepo would always use $em. Just for kicks, try extending the repository from EntityRepository and see if that makes any difference. Just a guess.

    – Cerad
    Nov 21 '18 at 20:22











  • And while unrelated, using the same event manager for different entity managers might also cause some amusing problems. Never tried it. I would probably avoid making my EntityManagerProvider dependent on the default entity manager. I see what you are trying to do. Just not sure if it will actually work or not.

    – Cerad
    Nov 21 '18 at 20:34











  • I should think about another solution, but extending from EntityRepository looks work! Thank you.

    – Jarek Kowol
    Nov 21 '18 at 20:56














  • 1





    You are sure it is the exact same $em instance? Does your repository extend from ServiceEntityRepository?

    – Cerad
    Nov 21 '18 at 20:16













  • @Cerad, yes extend ServiceEntityRepository...

    – Jarek Kowol
    Nov 21 '18 at 20:19






  • 1





    A ServiceEntityRepository find the first entity manager which supports your entity. That seems to be what is happening though I would have thought that $em->getRepo would always use $em. Just for kicks, try extending the repository from EntityRepository and see if that makes any difference. Just a guess.

    – Cerad
    Nov 21 '18 at 20:22











  • And while unrelated, using the same event manager for different entity managers might also cause some amusing problems. Never tried it. I would probably avoid making my EntityManagerProvider dependent on the default entity manager. I see what you are trying to do. Just not sure if it will actually work or not.

    – Cerad
    Nov 21 '18 at 20:34











  • I should think about another solution, but extending from EntityRepository looks work! Thank you.

    – Jarek Kowol
    Nov 21 '18 at 20:56








1




1





You are sure it is the exact same $em instance? Does your repository extend from ServiceEntityRepository?

– Cerad
Nov 21 '18 at 20:16







You are sure it is the exact same $em instance? Does your repository extend from ServiceEntityRepository?

– Cerad
Nov 21 '18 at 20:16















@Cerad, yes extend ServiceEntityRepository...

– Jarek Kowol
Nov 21 '18 at 20:19





@Cerad, yes extend ServiceEntityRepository...

– Jarek Kowol
Nov 21 '18 at 20:19




1




1





A ServiceEntityRepository find the first entity manager which supports your entity. That seems to be what is happening though I would have thought that $em->getRepo would always use $em. Just for kicks, try extending the repository from EntityRepository and see if that makes any difference. Just a guess.

– Cerad
Nov 21 '18 at 20:22





A ServiceEntityRepository find the first entity manager which supports your entity. That seems to be what is happening though I would have thought that $em->getRepo would always use $em. Just for kicks, try extending the repository from EntityRepository and see if that makes any difference. Just a guess.

– Cerad
Nov 21 '18 at 20:22













And while unrelated, using the same event manager for different entity managers might also cause some amusing problems. Never tried it. I would probably avoid making my EntityManagerProvider dependent on the default entity manager. I see what you are trying to do. Just not sure if it will actually work or not.

– Cerad
Nov 21 '18 at 20:34





And while unrelated, using the same event manager for different entity managers might also cause some amusing problems. Never tried it. I would probably avoid making my EntityManagerProvider dependent on the default entity manager. I see what you are trying to do. Just not sure if it will actually work or not.

– Cerad
Nov 21 '18 at 20:34













I should think about another solution, but extending from EntityRepository looks work! Thank you.

– Jarek Kowol
Nov 21 '18 at 20:56





I should think about another solution, but extending from EntityRepository looks work! Thank you.

– Jarek Kowol
Nov 21 '18 at 20:56












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419700%2funexpected-behavior-of-doctrine-entity-manager%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419700%2funexpected-behavior-of-doctrine-entity-manager%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini