Symfony2,Doctrine2,Entity Repository

时间:2013-12-27 03:32:01

标签: symfony doctrine-orm repository

我们有这样的存储库

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()......来做到这一点?在存储库中使用“实体管理器”是否正常?

1 个答案:

答案 0 :(得分:3)

您无法使用DQL插入文档中的说明

  

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

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

在Repo中使用enitity manager是完全可以的。你现在在做什么是正确的!