Skip to content
12 changes: 10 additions & 2 deletions src/session_decorators/identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,8 @@ public function getRelatedObjects( $object, $relatedClass, $relationName = null
*
* @return object($relatedClass)
*
* @throws ezcPersistentRelationNotFoundException
* if the given $object does not have a relation to $relatedClass.
* @throws ezcPersistentRelatedObjectNotFoundException
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove this comment is wrong -- it still can throw this exception ;-)

* if there is no object of $relatedClass found for $object
*/
public function getRelatedObject( $object, $relatedClass, $relationName = null )
{
Expand All @@ -692,6 +692,14 @@ public function getRelatedObject( $object, $relatedClass, $relationName = null )
$relatedClass,
$relationName
);
if ( empty( $relObjs ) ) {
// no object found, so throw same exception like it is thrown by
// ezcPersistentSession
throw new ezcPersistentRelatedObjectNotFoundException(
$object,
$relatedClass
);
}
return reset( $relObjs );
}

Expand Down
1 change: 1 addition & 0 deletions tests/data/relation_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function insertData( $db = null )
$db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Theodor" ) . ", " . $db->quote( "Gopher" ) . ", 2)" );
$db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Frederick" ) . ", " . $db->quote( "Ajax" ) . ", 1)" );
$db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Raymond" ) . ", " . $db->quote( "Socialweb" ) . ", 1)" );
$db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Pete" ) . ", " . $db->quote( "Javascript" ) . ", 0)" );

$db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons_addresses" ) . " ( " . $db->quoteIdentifier( "person_id" ) . ", " . $db->quoteIdentifier( "address_id" ) . ") VALUES ( 1, 1)" );
$db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons_addresses" ) . " ( " . $db->quoteIdentifier( "person_id" ) . ", " . $db->quoteIdentifier( "address_id" ) . ") VALUES ( 1, 2)" );
Expand Down
9 changes: 9 additions & 0 deletions tests/persistent_session_identity_decorator/relation_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ public function testGetRelatedObjectEmployer1Refetch()
$this->assertEquals( $employer1, $employer2 );
}

/**
* @expectedException ezcPersistentRelatedObjectNotFoundException
*/
public function testGetRelatedObjectNotFound()
{
$person = $this->idSession->load( "RelationTestPerson", 4 );
$this->idSession->getRelatedObject( $person, "RelationTestEmployer" );
}

public function testAddRelatedObjectEmployerFailureReverse()
{
$person = $this->idSession->load( "RelationTestPerson", 2 );
Expand Down