mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
4c4c20919f
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60407 467b73ca-7a2a-4603-9d3b-597d59a354a9
74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
class DataObjectDecoratorTest extends SapphireTest {
|
|
static $fixture_file = 'sapphire/tests/DataObjectTest.yml';
|
|
|
|
function testOneToManyAssociationWithDecorator() {
|
|
// Fails in RestfulServerTest
|
|
// Error: Object::__call() Method 'RelatedObjects' not found in class 'RestfulServerTest_Comment'
|
|
$contact = new DataObjectDecoratorTest_Member();
|
|
$contact->Website = "http://www.example.com";
|
|
|
|
$object = new DataObjectDecoratorTest_RelatedObject();
|
|
$object->FieldOne = "Lorem ipsum dolor";
|
|
$object->FieldTwo = "Random notes";
|
|
|
|
// The following code doesn't currently work:
|
|
// $contact->RelatedObjects()->add($object);
|
|
// $contact->write();
|
|
|
|
// Instead we have to do the following
|
|
$contact->write();
|
|
$object->ContactID = $contact->ID;
|
|
$object->write();
|
|
|
|
unset($contact);
|
|
|
|
$contact = DataObject::get_one("DataObjectDecoratorTest_Member", "Website='http://www.example.com'");
|
|
|
|
$this->assertType('DataObjectDecoratorTest_RelatedObject', $contact->RelatedObjects()->First());
|
|
$this->assertEquals("Lorem ipsum dolor", $contact->RelatedObjects()->First()->FieldOne);
|
|
$this->assertEquals("Random notes", $contact->RelatedObjects()->First()->FieldTwo);
|
|
$contact->delete();
|
|
}
|
|
|
|
}
|
|
|
|
class DataObjectDecoratorTest_Member extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
"Name" => "Text",
|
|
"Email" => "Text"
|
|
);
|
|
|
|
}
|
|
|
|
class DataObjectDecoratorTest_ContactRole extends DataObjectDecorator implements TestOnly {
|
|
|
|
function extraDBFields() {
|
|
return array(
|
|
'db' => array(
|
|
'Website' => 'Text',
|
|
'Phone' => 'Varchar(255)',
|
|
),
|
|
'has_many' => array(
|
|
'RelatedObjects' => 'DataObjectDecoratorTest_RelatedObject'
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
class DataObjectDecoratorTest_RelatedObject extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
"FieldOne" => "Text",
|
|
"FieldTwo" => "Text"
|
|
);
|
|
|
|
static $has_one = array(
|
|
"Contact" => "DataObjectDecoratorTest_Member"
|
|
);
|
|
|
|
}
|
|
|
|
DataObject::add_extension('DataObjectDecoratorTest_Member', 'DataObjectDecoratorTest_ContactRole');
|
|
?>
|