【问题标题】:multiple entitymanager symfony多实体管理器 symfony
【发布时间】:2017-10-18 16:45:30
【问题描述】:

在我只使用一个实体管理器与我的数据库交互之前。

在我添加 2 个新实体管理器(一个实体管理器插入数据和一个实体管理器选择数据)之前,一切都运行良好

    -> I want to make a replication of my database later.

我想,我一个人可以解决问题,但我只是想了解为什么会出现这个错误:

    A new entity was found through the relationship 'AppBundle\Entity\Userinterest#user'
    that was not configured to cascade persist operations for entity: my_username. 
    To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity 
    or configure cascade persist this association in the mapping for example 
    @ManyToOne(..,cascade={"persist"})

我想,我理解这个问题:):

来自 FOSUserBundle 的我的用户已通过默认实体管理器进行身份验证。

    $userinteret->setUser($this->getUser());
    $em = $this->getDoctrine()->getManager();
    $em->persist($userinteret);
    $em->flush();

然后,我不知道token_storage是如何存储的。但是因为我使用的是token_storage中的用户,所以我认为probem来自那里。

【问题讨论】:

  • 只是一个猜测,但您可能正在使用一个经理拉实体,然后尝试使用第二个经理来坚持。实体管理器是自包含的。他们不知道其他经理做了什么。所以我怀疑你的方法会以失败告终。如果您怀疑发生了其他事情,请使用几行更新您的问题,显示您如何尝试持久化问题实体。
  • 是的。 FOSUserBundle 正在为用户加载一个实体管理器,而您可能正在尝试使用第二个实体管理器。您现在可能正在尝试各种工作。

标签: symfony entitymanager


【解决方案1】:

试试看:

    // All 3 return the "default" entity manager
    $em = $this->getDoctrine()->getManager();
    $em = $this->getDoctrine()->getManager('default');
    $em = $this->get('doctrine.orm.default_entity_manager');

    // Both of these return the "customer" entity manager
    $customerEm = $this->getDoctrine()->getManager('customer');
    $customerEm = $this->get('doctrine.orm.customer_entity_manager');

How to Work with multiple Entity Managers and Connections

【讨论】:

  • 问题似乎不是关于如何访问多个实体管理器,而是持续存在错误。你能说明你发布的代码是如何消除错误的吗?
猜你喜欢
  • 2017-04-13
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
相关资源
最近更新 更多