Doctrine\ORM\UnitOfWork::propertyChanged PHP Method

propertyChanged() public method

Notifies this UnitOfWork of a property change in an entity.
public propertyChanged ( object $entity, string $propertyName, mixed $oldValue, mixed $newValue )
$entity object The entity that owns the property.
$propertyName string The name of the property that changed.
$oldValue mixed The old value of the property.
$newValue mixed The new value of the property.
    public function propertyChanged($entity, $propertyName, $oldValue, $newValue)
    {
        $oid = spl_object_hash($entity);
        $class = $this->em->getClassMetadata(get_class($entity));

        $isAssocField = isset($class->associationMappings[$propertyName]);

        if ( ! $isAssocField && ! isset($class->fieldMappings[$propertyName])) {
            return; // ignore non-persistent fields
        }

        // Update changeset and mark entity for synchronization
        $this->entityChangeSets[$oid][$propertyName] = array($oldValue, $newValue);
        if ( ! isset($this->scheduledForDirtyCheck[$class->rootEntityName][$oid])) {
            $this->scheduleForDirtyCheck($entity);
        }
    }

Usage Example

 /**
  * Flush for update
  *
  * @param object $entity
  * @param \Doctrine\ORM\EntityManager $entityManager
  * @param \Doctrine\ORM\UnitOfWork $unitOfWork
  */
 protected function onFlushForUpdates(&$entity, EntityManager &$entityManager, UnitOfWork &$unitOfWork)
 {
     $className = get_class($entity);
     $this->initializeAnnotationsForEntity($className);
     if (count(self::$storedProperties[$className]['annotations']) > 0) {
         foreach (self::$storedProperties[$className]['annotations'] as $propertyName => &$annotations) {
             /* @var $annotation Traceable */
             foreach ($annotations as $annotation) {
                 $run = $annotation->on === 'update';
                 if ($annotation->on === 'change') {
                     $changeSet = $unitOfWork->getEntityChangeSet($entity);
                     if (isset($changeSet[$annotation->field])) {
                         $type = $this->getPropertyType($className, $annotation->field);
                         $values = $annotation->getFieldValues($type, $entity);
                         $run = count($values) === 0 || in_array($changeSet[$annotation->field][1], $values);
                     }
                 }
                 if ($run) {
                     list($oldValue, $value) = $this->updateEntityPropertyValue($entity, $className, $propertyName, $annotation);
                     $entityManager->persist($entity);
                     $unitOfWork->propertyChanged($entity, $propertyName, $oldValue, $value);
                     $unitOfWork->scheduleExtraUpdate($entity, array($propertyName => array($oldValue, $value)));
                     break;
                 }
             }
         }
     }
 }
All Usage Examples Of Doctrine\ORM\UnitOfWork::propertyChanged