【问题标题】:Symfony2, Doctrine2, Entity RepositorySymfony2,Doctrine2,实体存储库
【发布时间】:2013-12-27 03:32:00
【问题描述】:

我们有这样的存储库

class CronInfoCharacterInfoRepository extends EntityRepository
{
    /**
     * @param string|integer $characterID
     * @return void
     */
    public function add($characterID)
    {
        if (!$this->find($characterID)) {

            $oEntry = new CronInfoCharacterInfo();
            $oEntry->setCharacterID($characterID);

            $this->getEntityManager()->persist($oEntry);
            $this->getEntityManager()->flush();
        }
    }
}

但我想通过 DQL 插入新条目。我怎样才能通过 $this->createQueryBuilder()... 做到这一点?在存储库中使用“实体管理器”是否正常?

【问题讨论】:

    标签: symfony doctrine-orm repository


    【解决方案1】:

    您不能使用 DQL 插入文档中提到的内容

    DQL 中不允许使用 INSERT 语句,因为实体及其 关系必须通过 EntityManager#persist() 以确保您的对象模型的一致性。

    http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html

    在 Repo 中使用实体管理器是完全可以的。你现在的做法是对的!

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2012-06-19
      • 2013-04-20
      • 2012-01-12
      • 2015-01-18
      相关资源
      最近更新 更多