def _createEntity(self, sgEntityType, sgData, sgSyncFields=None):
    '''
    Internal function!

    Locks the connection and if the Entity has an ID the cache is checked to see
    if it already has an SgEntity and if so it returns it otherwise it creates one.
    '''

    ShotgunORM.LoggerConnection.debug('%(connection)s._createEntity(...)', {'connection': self})
    ShotgunORM.LoggerConnection.debug('    * sgEntityType: %(entityName)s', {'entityName': sgEntityType})
    ShotgunORM.LoggerConnection.debug('    * sgData: %(sgData)s', {'sgData': sgData})

    with self:
      sgData = dict(sgData)

      factory = self.classFactory()

      result = None

      eId = None

      if sgData.has_key('id'):
        eId = int(sgData['id'])
      else:
        eId = -1

      if not self.__entityCache.has_key(sgEntityType):
        self.__entityCache[sgEntityType] = {}

      # Return immediately if the Entity does not exist.
      if eId <= -1:
        sgData['id'] = -id(result)

        result = factory.createEntity(self, sgEntityType, sgData)

        ShotgunORM.onEntityCreate(result)

        return result

      onCreate = False

      # Check the cache and if its found update any non-valid fields that
      # have data contained in the passed sgData.  If not found create the
      # Entity and add it to the cache.]
      if self.__entityCache[sgEntityType].has_key(eId):
        result = self.__entityCache[sgEntityType][eId]['entity']

        if result != None:
          result = result()

        if result == None:
          cacheData = self.__entityCache[sgEntityType][eId]['cache']

          tmpData = {
            'id': eId,
            'type': sgEntityType
          }

          tmpData.update(cacheData)

          result = factory.createEntity(self, sgEntityType, tmpData)

          self.__entityCache[sgEntityType][eId]['entity'] = weakref.ref(result)

          onCreate = True

        with result:
          del sgData['id']
          del sgData['type']

          for field, value in sgData.items():
            fieldObj = result.field(field)

            if fieldObj.isValid() or fieldObj.hasCommit() or fieldObj.hasSyncUpdate():
              continue

            fieldObj.invalidate()

            fieldObj._updateValue = value

            fieldObj.setHasSyncUpdate(True)
      else:
        result = factory.createEntity(self, sgEntityType, sgData)

        self.__entityCache[sgEntityType][eId] = {
          'entity': weakref.ref(result),
          'cache': {}
        }

        onCreate = True

      if sgSyncFields != None:
        result.sync(sgSyncFields, ignoreValid=True, ignoreWithUpdate=True, backgroundPull=True)

      if onCreate:
        ShotgunORM.onEntityCreate(result)

      return result
  def _createEntity(self, sgEntityType, sgData, sgSyncFields=None):
    '''
    Internal function!

    Locks the connection and if the Entity has an ID the cache is checked to see
    if it already has an SgEntity and if so it returns it otherwise it creates
    one.
    '''

    ShotgunORM.LoggerConnection.debug(
      '%(connection)s._createEntity(...)', {'connection': self}
    )

    ShotgunORM.LoggerConnection.debug(
      '    * sgEntityType: %(entityName)s', {'entityName': sgEntityType}
    )

    ShotgunORM.LoggerConnection.debug(
      '    * sgData: %(sgData)s', {'sgData': sgData}
    )

    with self:
      sgData = dict(sgData)

      factory = self.classFactory()

      result = None

      eId = None

      if sgData.has_key('id'):
        eId = int(sgData['id'])
      else:
        eId = -1

      if not self.__entityCache.has_key(sgEntityType):
        self.__entityCache[sgEntityType] = {}

      # Return immediately if the Entity does not exist.
      if eId <= -1:
        result = factory.createEntity(self, sgEntityType, sgData)

        ShotgunORM.onEntityCreate(result)

        return result

      onCreate = False

      # Check the cache and if its found update any non-valid fields that
      # have data contained in the passed sgData.  If not found create the
      # Entity and add it to the cache.]
      if self.__entityCache[sgEntityType].has_key(eId):
        cacheData = self.__entityCache[sgEntityType][eId]
        result = cacheData['entity']

        if result != None:
          result = result()

        if result == None:
          tmpData = {
            'id': eId,
            'type': sgEntityType
          }

          tmpData.update(cacheData['cache'])

          result = factory.createEntity(self, sgEntityType, tmpData)

          result._SgEntity__caching = cacheData['cache_state']

          cacheData['entity'] = weakref.ref(result)

          onCreate = True

        with result:
          del sgData['id']
          del sgData['type']

          for field, value in sgData.items():
            fieldObj = result.field(field)

            if (
              fieldObj == None or
              fieldObj.isValid() or
              fieldObj.hasCommit() or
              fieldObj.hasSyncUpdate()
            ):
              continue

            fieldObj.invalidate()

            fieldObj._updateValue = value

            fieldObj.setHasSyncUpdate(True)
      else:
        result = factory.createEntity(self, sgEntityType, sgData)

        self.__entityCache[sgEntityType][eId] = {
          'entity': weakref.ref(result),
          'cache': {},
          'cache_state': -1
        }

        onCreate = True

      if sgSyncFields != None:
        result.sync(
          sgSyncFields,
          ignoreValid=True,
          ignoreWithUpdate=True,
          backgroundPull=True
        )

      if onCreate:
        ShotgunORM.onEntityCreate(result)

      return result