Author: fh
Date: Tue May  8 16:03:02 2007
New Revision: 5145

Log:
- Fixed issue: #010725: PersistentObject saveOrUpdate() using manualgenerator 
will trigger one extra query to check if an object is persistent (save() and 
update() do this as well just moments later...)
        

Modified:
    trunk/PersistentObject/ChangeLog
    trunk/PersistentObject/src/persistent_session.php
    trunk/PersistentObject/tests/string_identifier_test.php
Modified: trunk/PersistentObject/ChangeLog
==============================================================================
--- trunk/PersistentObject/ChangeLog (original)
+++ trunk/PersistentObject/ChangeLog Tue May  8 16:03:02 2007
@@ -2,6 +2,7 @@
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 - Fixed issue: #010152: Persistent Object and manual generator: string primary keys
+- Fixed issue: #010725: PersistentObject saveOrUpdate() using manualgenerator will trigger one extra query to check if an object is persistent (save() and update() do this as well just moments later...)
 	
 	
 1.3beta1 - Monday 07 May 2007

Modified: trunk/PersistentObject/src/persistent_session.php
==============================================================================
--- trunk/PersistentObject/src/persistent_session.php (original)
+++ trunk/PersistentObject/src/persistent_session.php Tue May  8 16:03:02 2007
@@ -868,13 +868,31 @@
      */
     public function save( $pObject )
     {
+        $this->saveInternal( $pObject );
+    }
+
+    /**
+     * Saves the new persistent object $pObject to the database using an INSERT INTO query.
+     *
+     * If $doPersistenceCheck is set this function will check if the object is persistent before
+     * saving. If not, the check is omitted. The correct ID is set to $pObject.
+     *
+     * @throws ezcPersistentException if $pObject is not of a valid persistent object type.
+     * @throws ezcPersistentException if $pObject is already stored to the database.
+     * @throws ezcPersistentException if it was not possible to generate a unique identifier for the new object
+     * @throws ezcPersistentException if the insert query failed.
+     * @param object $pObject
+     * @param boolean $doPersistentCheck
+     * @return void
+     */
+    private function saveInternal( $pObject, $doPersistenceCheck = true, $idGenerator = null )
+    {
         $def = $this->definitionManager->fetchDefinition( get_class( $pObject ) );// propagate exception
         $state = $this->filterAndCastState( $pObject->getState(), $def );
         $idValue = $state[$def->idProperty->propertyName];
 
         // fetch the id generator
-        $idGenerator = null;
-        if ( ezcBaseFeatures::classExists( $def->idProperty->generator->class ) )
+        if ( $idGenerator == null && ezcBaseFeatures::classExists( $def->idProperty->generator->class ) )
         {
             $idGenerator = new $def->idProperty->generator->class;
             if ( !( $idGenerator instanceof ezcPersistentIdentifierGenerator ) )
@@ -884,7 +902,7 @@
             }
         }
 
-        if ( $idGenerator->checkPersistence( $def, $this->database, $state ) )
+        if ( $doPersistenceCheck == true && $idGenerator->checkPersistence( $def, $this->database, $state ) )
         {
             $class = get_class( $pObject );
             throw new ezcPersistentObjectAlreadyPersistentException( $class );
@@ -967,11 +985,11 @@
 
         if ( !$idGenerator->checkPersistence( $def, $this->database, $state ) )
         {
-            $this->save( $pObject );
+            $this->saveInternal( $pObject, false, $idGenerator );
         }
         else
         {
-            $this->update( $pObject );
+            $this->updateInternal( $pObject, false );
         }
     }
 
@@ -986,6 +1004,24 @@
      */
     public function update( $pObject )
     {
+        $this->updateInternal( $pObject );
+    }
+
+    /**
+     * Saves the new persistent object $pObject to the database using an UPDATE query.
+     *
+     * If $doPersistenceCheck is set this function will check if the object is persistent before
+     * saving. If not, the check is omitted.
+     *
+     * @throws ezcPersistentDefinitionNotFoundException if $pObject is not of a valid persistent object type.
+     * @throws ezcPersistentObjectNotPersistentException if $pObject is not stored in the database already.
+     * @throws ezcPersistentQueryException
+     * @param object $pObject
+     * @param boolean $pObject
+     * @return void
+     */
+    private function updateInternal( $pObject, $doPersistenceCheck = true )
+    {
         $def = $this->definitionManager->fetchDefinition( get_class( $pObject ) ); // propagate exception
         $state = $this->filterAndCastState( $pObject->getState(), $def );
         $idValue = $state[$def->idProperty->propertyName];
@@ -1002,7 +1038,7 @@
             }
         }
 
-        if ( !$idGenerator->checkPersistence( $def, $this->database, $state ) )
+        if ( $doPersistenceCheck == true && !$idGenerator->checkPersistence( $def, $this->database, $state ) )
         {
             $class = get_class( $pObject );
             throw new ezcPersistentObjectNotPersistentException( get_class( $pObject ) );

Modified: trunk/PersistentObject/tests/string_identifier_test.php
==============================================================================
--- trunk/PersistentObject/tests/string_identifier_test.php (original)
+++ trunk/PersistentObject/tests/string_identifier_test.php Tue May  8 16:03:02 2007
@@ -91,7 +91,6 @@
         $object->id = "id";
         $object->data = "42";
         $this->session->saveOrUpdate( $object );
-
         $this->assertEquals( "id", $object->id );
 
         $object2 = $this->session->loadIfExists( 'MainTable', "id" );
@@ -111,7 +110,6 @@
         $object2 = $this->session->loadIfExists( 'MainTable', "id" );
         $this->assertNotEquals( NULL, $object2 );
         $this->assertEquals( "42", $object2->data );
-
         $object2->data = "99";
         $this->session->saveOrUpdate( $object2 );
 

-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to