Author: ts
Date: Fri Jan 18 17:52:59 2008
New Revision: 7189

Log:
- Implemented addRelatedObject() support for multi-relations.

Modified:
    trunk/PersistentObject/src/handlers/save_handler.php
    trunk/PersistentObject/src/persistent_session.php
    trunk/PersistentObject/tests/data/multirelationtestperson.php
    trunk/PersistentObject/tests/multi_relation_test.php

Modified: trunk/PersistentObject/src/handlers/save_handler.php
==============================================================================
--- trunk/PersistentObject/src/handlers/save_handler.php [iso-8859-1] (original)
+++ trunk/PersistentObject/src/handlers/save_handler.php [iso-8859-1] Fri Jan 
18 17:52:59 2008
@@ -131,7 +131,7 @@
      * @throws ezcPersistentRelationNotFoundException
      *         if the deisred relation is not defined.
      */
-    public function addRelatedObject( $object, $relatedObject )
+    public function addRelatedObject( $object, $relatedObject, $relationName = 
null )
     {
         $class        = get_class( $object );
         $relatedClass = get_class( $relatedObject );
@@ -140,7 +140,7 @@
         $objectState        = $this->session->getObjectState( $object );
         $relatedObjectState = $this->session->getObjectState( $relatedObject );
 
-        // Sanity checks.
+        // Sanity check
         if ( !isset( $def->relations[$relatedClass] ) )
         {
             throw new ezcPersistentRelationNotFoundException(
@@ -148,10 +148,29 @@
                 $relatedClass
             );
         }
-        if (
-            isset( $def->relations[$relatedClass]->reverse ) 
-            && $def->relations[$relatedClass]->reverse === true
-        )
+
+        $relation = $def->relations[$relatedClass];
+        
+        // New multi-relations for a single class
+        if ( $relation instanceof ezcPersistentRelationCollection )
+        {
+            if ( $relationName === null )
+            {
+                throw new ezcPersistentUndeterministicRelationException( 
$relatedClass );
+            }
+            if ( !isset( $relation[$relationName] ) )
+            {
+                throw new ezcPersistentRelationNotFoundException(
+                    $class,
+                    $relatedClass,
+                    $relationName
+                );
+            }
+            $relation = $relation[$relationName];
+        }
+
+        // Another sanity check
+        if ( isset( $relation->reverse ) && $relation->reverse )
         {
             throw new ezcPersistentRelationOperationNotSupportedException(
                 $class,
@@ -162,7 +181,6 @@
         }
 
         $relatedDef = $this->definitionManager->fetchDefinition( $relatedClass 
);
-        $relation   = $def->relations[$relatedClass];
 
         switch ( get_class( $relation ) )
         {

Modified: trunk/PersistentObject/src/persistent_session.php
==============================================================================
--- trunk/PersistentObject/src/persistent_session.php [iso-8859-1] (original)
+++ trunk/PersistentObject/src/persistent_session.php [iso-8859-1] Fri Jan 18 
17:52:59 2008
@@ -399,8 +399,14 @@
      * record is stored automatically and there is no need to store
      * $relatedObject explicitly after establishing the relation.
      *
+     * If there are multiple relations defined between the class of $object and
+     * $relatedObject (via [EMAIL PROTECTED] 
ezcPersistentRelationCollection}), the
+     * $relationName parameter becomes mandatory to determine, which exact
+     * relation should be used.
+     *
      * @param object $object
      * @param object $relatedObject
+     * @param string $relationName
      *
      * @throws ezcPersistentRelationOperationNotSupportedException
      *         if a relation to create is marked as "reverse" [EMAIL PROTECTED]
@@ -408,9 +414,9 @@
      * @throws ezcPersistentRelationNotFoundException
      *         if the deisred relation is not defined.
      */
-    public function addRelatedObject( $object, $relatedObject )
-    {
-        return $this->saveHandler->addRelatedObject( $object, $relatedObject );
+    public function addRelatedObject( $object, $relatedObject, $relatioName = 
null )
+    {
+        return $this->saveHandler->addRelatedObject( $object, $relatedObject, 
$relatioName );
     }
 
     /**

Modified: trunk/PersistentObject/tests/data/multirelationtestperson.php
==============================================================================
--- trunk/PersistentObject/tests/data/multirelationtestperson.php [iso-8859-1] 
(original)
+++ trunk/PersistentObject/tests/data/multirelationtestperson.php [iso-8859-1] 
Fri Jan 18 17:52:59 2008
@@ -43,6 +43,7 @@
 $relations['mothers_children']->columnMap = array(
     new ezcPersistentSingleTableMap( 'id', 'mother' )
 );
+$relations['mothers_children']->cascade = true;
 
 $relations['mother'] = new ezcPersistentManyToOneRelation(
     'PO_person',

Modified: trunk/PersistentObject/tests/multi_relation_test.php
==============================================================================
--- trunk/PersistentObject/tests/multi_relation_test.php [iso-8859-1] (original)
+++ trunk/PersistentObject/tests/multi_relation_test.php [iso-8859-1] Fri Jan 
18 17:52:59 2008
@@ -217,6 +217,76 @@
             'Siblings not correctly found for MultiRelationTestPerson 3'
         );
     }
+
+    public function testSave()
+    {
+        $newChild = new MultiRelationTestPerson();
+        $newChild->name = "New child";
+
+        $this->session->save( $newChild );
+
+        $this->assertEquals(
+            6,
+            $newChild->id,
+            'New MultiRelationTestPerson saved with incorrect ID.'
+        );
+    }
+
+    public function testAddRelatedObjectOneToManySuccess()
+    {
+        $newChild = new MultiRelationTestPerson();
+        $newChild->name = "New child";
+
+        $this->session->save( $newChild );
+
+        $mother = $this->session->load( 'MultiRelationTestPerson', 1 );
+
+        $this->session->addRelatedObject( $mother, $newChild, 
'mothers_children' );
+        
+        $this->assertEquals(
+            $mother->id,
+            $newChild->mother,
+            'New MultiRelationTestPerson child not added correctly'
+        );
+    }
+
+    public function testAddRelatedObjectManyToManySuccess()
+    {
+        $newSibling = new MultiRelationTestPerson();
+        $newSibling->name = "New child";
+
+        $this->session->save( $newSibling );
+
+        $sibling = $this->session->load( 'MultiRelationTestPerson', 3 );
+
+        $this->session->addRelatedObject( $sibling, $newSibling, 'siblings' );
+
+        $q = $this->session->database->createSelectQuery();
+        $q->select( '*' )
+          ->from( 'PO_sibling' )
+          ->where(
+            $q->expr->eq(
+                $this->session->database->quoteIdentifier( 'sibling' ),
+                $q->bindValue( $newSibling->id )
+            )
+          );
+
+        $stmt = $q->prepare();
+        $stmt->execute();
+        $rows = $stmt->fetchAll( PDO::FETCH_ASSOC );
+
+        $this->assertEquals(
+            1,
+            count( $rows ),
+            'Incorrect number of relation records.'
+        );
+
+        $this->assertEquals(
+            $sibling->id,
+            $rows[0]['person'],
+            'Incorrect perso ID in relation record.'
+        );
+    }
 }
 
 ?>


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

Reply via email to