BUGFIX Fixed many_many relations querying in RestfulServer

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@81346 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-07-08 08:28:17 +00:00
parent c389c01f9c
commit e54a2e75e6
3 changed files with 38 additions and 11 deletions

View File

@ -508,6 +508,8 @@ class RestfulServer extends Controller {
$query = $obj->{"{$relationName}Query"}(null, $sort, null, $limit);
$relationClass = $obj->{"{$relationName}Class"}();
} elseif($relationClass = $obj->many_many($relationName)) {
// many_many() returns different notation
$relationClass = $relationClass[1];
$query = $obj->getManyManyComponentsQuery($relationName);
} elseif($relationClass = $obj->has_many($relationName)) {
$query = $obj->getComponentsQuery($relationName);

View File

@ -91,6 +91,23 @@ class RestfulServerTest extends SapphireTest {
$this->assertEquals($ratingsArr[1]['@attributes']['id'], $rating2->ID);
}
public function testGETManyManyRelationshipsXML() {
// author4 has related authors author2 and author3
$author2 = $this->objFromFixture('RestfulServerTest_Author', 'author2');
$author3 = $this->objFromFixture('RestfulServerTest_Author', 'author3');
$author4 = $this->objFromFixture('RestfulServerTest_Author', 'author4');
$url = "/api/v1/RestfulServerTest_Author/" . $author4->ID . '/RelatedAuthors';
$response = Director::test($url, null, null, 'GET');
$this->assertEquals($response->getStatusCode(), 200);
$arr = Convert::xml2array($response->getBody());
$authorsArr = $arr['RestfulServerTest_Author'];
$this->assertEquals(count($authorsArr), 2);
$this->assertEquals($authorsArr[0]['ID'], $author2->ID);
$this->assertEquals($authorsArr[1]['ID'], $author3->ID);
}
public function testPUTWithFormEncoded() {
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
$_SERVER['PHP_AUTH_PW'] = 'editor';
@ -410,6 +427,7 @@ class RestfulServerTest_Author extends DataObject implements TestOnly {
static $many_many = array(
'RelatedPages' => 'RestfulServerTest_Page',
'RelatedAuthors' => 'RestfulServerTest_Author',
);
static $has_many = array(

View File

@ -43,6 +43,13 @@ RestfulServerTest_Comment:
RestfulServerTest_Author:
author1:
FirstName: Author 1
author2:
FirstName: Author 2
author3:
Firstname: Author 3
author4:
FirstName: Author 4
RelatedAuthors: =>RestfulServerTest_Author.author2,=>RestfulServerTest_Author.author3
RestfulServerTest_AuthorRating:
rating1:
Rating: 3