Skip to content
Open
11 changes: 11 additions & 0 deletions src/session_decorators/identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ public function getRelatedObjects( $object, $relatedClass, $relationName = null
*
* @throws ezcPersistentRelationNotFoundException

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 the given $object does not have a relation to $relatedClass.
*
* @throws ezcPersistentRelatedObjectNotFoundException
* if there is no object of $relatedClass found for $object
*/
public function getRelatedObject( $object, $relatedClass, $relationName = null )
{
Expand All @@ -692,6 +695,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
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public function testFindOneLevelOneRelationRestrictions()
);

$fakeFind = $this->session->createFindQuery( 'RelationTestPerson' );
// ignore Persons where employer is not set
$fakeFind->where(
$fakeFind->expr->neq('employer', 0)
);
$fakeRes = $this->session->find( $fakeFind );

$this->assertEquals(
Expand Down Expand Up @@ -488,6 +492,10 @@ public function testFindOneLevelMultiRelationRestrictions()
);

$fakeFind = $this->session->createFindQuery( 'RelationTestPerson' );
// ignore Persons where employer is not set
$fakeFind->where(
$fakeFind->expr->neq('employer', 0)
);
$fakeRes = $this->session->find( $fakeFind );

$this->assertEquals(
Expand Down Expand Up @@ -622,6 +630,10 @@ public function testFindMultiLevelSingleRelationRestrictions()
);

$fakeFind = $this->session->createFindQuery( 'RelationTestPerson' );
// ignore Persons where employer is not set
$fakeFind->where(
$fakeFind->expr->neq('employer', 0)
);
$fakeRes = $this->session->find( $fakeFind );

$this->assertEquals(
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
10 changes: 5 additions & 5 deletions tests/relations/one_to_many_relation_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public function testAddRelatedPersonToEmployer2SaveSuccess()
$this->session->save( $person );

$res = RelationTestPerson::__set_state(array(
'id' => 4,
'id' => 5,
'firstname' => 'Jan',
'surname' => 'Soap',
'employer' => 2,
Expand Down Expand Up @@ -457,14 +457,14 @@ public function testAddRelatedPersonsToEmployer2SaveSuccess()
$res = array(
0 =>
RelationTestPerson::__set_state(array(
'id' => 4,
'id' => 5,
'firstname' => 'Tobias',
'surname' => 'Preprocess',
'employer' => 2,
)),
1 =>
RelationTestPerson::__set_state(array(
'id' => 5,
'id' => 6,
'firstname' => 'Jan',
'surname' => 'Soap',
'employer' => 2,
Expand Down Expand Up @@ -594,9 +594,9 @@ public function testDeleteEmployer2CascadePersonCascadeBirthdaySuccess()
$stmt->execute();

$this->assertEquals(
2,
3,
$stmt->fetchColumn(),
"Persons cascaded from employer not deletec correctly."
"Persons cascaded from employer not deleted correctly."
);
unset( $q, $stmt );

Expand Down