Sfoglia il codice sorgente

[loggable] fixed a bug with identifier extraction

gediminasm 14 anni fa
parent
commit
5f0486e248

+ 7 - 7
lib/Gedmo/Loggable/Document/Repository/LogEntryRepository.php

@@ -8,7 +8,7 @@ use Doctrine\ODM\MongoDB\DocumentRepository,
 /**
  * The LogEntryRepository has some useful functions
  * to interact with log entries.
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo\Loggable\Document\Repository
  * @subpackage LogEntryRepository
@@ -16,14 +16,14 @@ use Doctrine\ODM\MongoDB\DocumentRepository,
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class LogEntryRepository extends DocumentRepository
-{    
+{
     /**
      * Loads all log entries for the
      * given $document
-     * 
+     *
      * @param object $document
      * @return array
-     */ 
+     */
     public function getLogEntries($document)
     {
         $objectClass = get_class($document);
@@ -42,7 +42,7 @@ class LogEntryRepository extends DocumentRepository
         }
         return $result;
     }
-    
+
     /**
      * Reverts given $document to $revision by
      * restoring all fields from that $revision.
@@ -60,14 +60,14 @@ class LogEntryRepository extends DocumentRepository
         $objectMeta = $this->dm->getClassMetadata($objectClass);
         $meta = $this->getClassMetadata();
         $objectId = $objectMeta->getReflectionProperty($objectMeta->identifier)->getValue($document);
-        
+
         $qb = $this->createQueryBuilder();
         $qb->field('objectId')->equals($objectId);
         $qb->field('objectClass')->equals($objectMeta->name);
         $qb->field('version')->lte($version);
         $qb->sort('version', 'ASC');
         $q = $qb->getQuery();
-        
+
         $logs = $q->execute();
         if ($logs instanceof Cursor) {
             $logs = $logs->toArray();

+ 2 - 1
lib/Gedmo/Loggable/LoggableListener.php

@@ -230,9 +230,10 @@ class LoggableListener extends MappedEventSubscriber
                         continue;
                     }
                     if ($meta->isSingleValuedAssociation($field) && $value) {
+                        $oid = spl_object_hash($value);
                         $value = $ea->extractIdentifier($om, $value, false);
                         if (!is_array($value)) {
-                            $this->pendingRelatedObjects[$value] = array(
+                            $this->pendingRelatedObjects[$oid] = array(
                                 'log' => $logEntry,
                                 'field' => $field
                             );

+ 3 - 2
lib/Gedmo/Mapping/Event/Adapter/ODM.php

@@ -60,13 +60,14 @@ class ODM implements AdapterInterface
      */
     public function extractIdentifier(DocumentManager $dm, $object, $single = true)
     {
+        $meta = $dm->getClassMetadata(get_class($object));
         if ($object instanceof Proxy) {
             $id = $dm->getUnitOfWork()->getDocumentIdentifier($object);
         } else {
-            $meta = $dm->getClassMetadata(get_class($object));
             $id = $meta->getReflectionProperty($meta->identifier)->getValue($object);
         }
-        if ($single) {
+
+        if ($single || !$id) {
             return $id;
         } else {
             return array($meta->identifier => $id);

+ 5 - 0
lib/Gedmo/Mapping/Event/Adapter/ORM.php

@@ -67,8 +67,13 @@ class ORM implements AdapterInterface
             $id = array();
             foreach ($meta->identifier as $name) {
                 $id[$name] = $meta->getReflectionProperty($name)->getValue($object);
+                // return null if one of identifiers is missing
+                if (!$id[$name]) {
+                    return null;
+                }
             }
         }
+
         if ($single) {
             $id = current($id);
         }

+ 1 - 1
lib/Gedmo/Sluggable/Mapping/Event/Adapter/ORM.php

@@ -35,7 +35,7 @@ final class ORM extends BaseAdapterORM implements SluggableAdapter
         // include identifiers
         $entityIdentifiers = $this->extractIdentifier($em, $object, false);
         $parameters = array();
-        foreach ($entityIdentifiers as $field => $value) {
+        foreach ((array)$entityIdentifiers as $field => $value) {
             if (strlen($value)) {
                 $qb->andWhere('rec.' . $field . ' <> :' . $field);
                 $parameters[$field] = $value;

+ 1 - 2
lib/Gedmo/Tree/Entity/Repository/ClosureTreeRepository.php

@@ -70,7 +70,6 @@ class ClosureTreeRepository extends AbstractTreeRepository
      */
     protected function validates()
     {
-        // Temporarily solution to validation problem with this class
-        return true;
+        return $this->listener->getStrategy($this->_em, $this->getClassMetadata()->name)->getName() === Strategy::CLOSURE;
     }
 }