Doctrine\ORM\UnitOfWork::getEntityState PHP Method

getEntityState() public method

Gets the state of an entity with regard to the current unit of work.
public getEntityState ( object $entity, integer $assume = null ) : integer
$entity object
$assume integer The state to assume if the state is not yet known (not MANAGED or REMOVED). This parameter can be set to improve performance of entity state detection by potentially avoiding a database lookup if the distinction between NEW and DETACHED is either known or does not matter for the caller of the method.
return integer The entity state.
    public function getEntityState($entity, $assume = null)
    {
        $oid = spl_object_hash($entity);
        if ( ! isset($this->entityStates[$oid])) {
            // State can only be NEW or DETACHED, because MANAGED/REMOVED states are known.
            // Note that you can not remember the NEW or DETACHED state in _entityStates since
            // the UoW does not hold references to such objects and the object hash can be reused.
            // More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it.
            if ($assume === null) {
                $class = $this->em->getClassMetadata(get_class($entity));
                $id = $class->getIdentifierValues($entity);
                if ( ! $id) {
                    return self::STATE_NEW;
                } else if ($class->isIdentifierNatural()) {
                    // Check for a version field, if available, to avoid a db lookup.
                    if ($class->isVersioned) {
                        if ($class->getFieldValue($entity, $class->versionField)) {
                            return self::STATE_DETACHED;
                        } else {
                            return self::STATE_NEW;
                        }
                    } else {
                        // Last try before db lookup: check the identity map.
                        if ($this->tryGetById($id, $class->rootEntityName)) {
                            return self::STATE_DETACHED;
                        } else {
                            // db lookup
                            if ($this->getEntityPersister(get_class($entity))->exists($entity)) {
                                return self::STATE_DETACHED;
                            } else {
                                return self::STATE_NEW;
                            }
                        }
                    }
                } else {
                    return self::STATE_DETACHED;
                }
            } else {
                return $assume;
            }
        }
        return $this->entityStates[$oid];
    }

Usage Example

 /**
  * Check if entity is in a valid state for operations.
  *
  * @param object $entity
  *
  * @return bool
  */
 protected function isValidEntityState($entity)
 {
     $entityState = $this->uow->getEntityState($entity, UnitOfWork::STATE_NEW);
     if ($entityState === UnitOfWork::STATE_NEW) {
         return false;
     }
     // If Entity is scheduled for inclusion, it is not in this collection.
     // We can assure that because it would have return true before on array check
     return !($entityState === UnitOfWork::STATE_MANAGED && $this->uow->isScheduledForInsert($entity));
 }
All Usage Examples Of Doctrine\ORM\UnitOfWork::getEntityState