diff --git a/code/extensions/MultiFormObjectDecorator.php b/code/extensions/MultiFormObjectDecorator.php index 6fb5052..b9e5e12 100644 --- a/code/extensions/MultiFormObjectDecorator.php +++ b/code/extensions/MultiFormObjectDecorator.php @@ -23,16 +23,27 @@ class MultiFormObjectDecorator extends DataExtension { private static $has_one = array( 'MultiFormSession' => 'MultiFormSession', ); - + + /** + * Augment any queries to MultiFormObjectDecorator and only + * return anything that isn't considered temporary. + */ public function augmentSQL(SQLQuery &$query) { - // If you're querying by ID, ignore the sub-site - this is a bit ugly... + $where = $query->getWhere(); + if(!$where && !$this->wantsTemporary($query)) { + $from = array_values($query->getFrom()); + $query->addWhere("{$from[0]}.MultiFormIsTemporary = 0"); + return; + } + if( - strpos($query->where[0], ".`ID` = ") === false - && strpos($query->where[0], ".ID = ") === false - && strpos($query->where[0], "ID = ") !== 0 + strpos($where[0], ".`ID` = ") === false + && strpos($where[0], ".ID = ") === false + && strpos($where[0], "ID = ") !== 0 && !$this->wantsTemporary($query) ) { - $query->where[] = "\"{$query->from[0]}\".\"MultiFormIsTemporary\" = 0"; + $from = array_values($query->getFrom()); + $query->addWhere("{$from[0]}.MultiFormIsTemporary = 0"); } } @@ -45,8 +56,10 @@ class MultiFormObjectDecorator extends DataExtension { * @return boolean */ protected function wantsTemporary($query) { - foreach($query->where as $whereClause) { - if($whereClause == "\"{$query->from[0]}\".\"MultiFormIsTemporary\" = 1") { + foreach($query->getWhere() as $whereClause) { + $from = array_values($query->getFrom()); + // SQLQuery will automatically add double quotes and single quotes to values, so check against that. + if($whereClause == "{$from[0]}.\"MultiFormIsTemporary\" = '1'") { return true; } } diff --git a/tests/MultiFormObjectDecoratorTest.php b/tests/MultiFormObjectDecoratorTest.php new file mode 100644 index 0000000..f935f93 --- /dev/null +++ b/tests/MultiFormObjectDecoratorTest.php @@ -0,0 +1,44 @@ + array('MultiFormObjectDecorator') + ); + + protected $extraDataObjects = array( + 'MultiFormObjectDecorator_DataObject' + ); + + public function testTemporaryDataFilteredQuery() { + $records = MultiFormObjectDecorator_DataObject::get() + ->map('Name') + ->toArray(); + + $this->assertContains('Test 1', $records); + $this->assertContains('Test 2', $records); + $this->assertNotContains('Test 3', $records); + + } + + public function testTemporaryDataQuery() { + $records = MultiFormObjectDecorator_DataObject::get() + ->filter(array('MultiFormIsTemporary' => 1)) + ->map('Name') + ->toArray(); + + $this->assertNotContains('Test 1', $records); + $this->assertNotContains('Test 2', $records); + $this->assertContains('Test 3', $records); + } + +} + +class MultiFormObjectDecorator_DataObject extends DataObject { + + private static $db = array( + 'Name' => 'Varchar' + ); + +} diff --git a/tests/MultiFormObjectDecoratorTest.yml b/tests/MultiFormObjectDecoratorTest.yml new file mode 100644 index 0000000..99bee26 --- /dev/null +++ b/tests/MultiFormObjectDecoratorTest.yml @@ -0,0 +1,10 @@ +MultiFormObjectDecorator_DataObject: + test-data-1: + Name: Test 1 + MultiFormIsTemporary: 0 + test-data-2: + Name: Test 2 + MultiFormIsTemporary: 0 + test-data-3: + Name: Test 3 + MultiFormIsTemporary: 1 diff --git a/tests/MultiFormTest.php b/tests/MultiFormTest.php index 94b9df5..b7e0bd8 100644 --- a/tests/MultiFormTest.php +++ b/tests/MultiFormTest.php @@ -91,7 +91,7 @@ class MultiFormTest extends FunctionalTest { session ID parameter"); Config::unnest(); - } +} } @@ -143,7 +143,7 @@ class MultiFormTest_StepOne extends MultiFormStep implements TestOnly { ); } } - + /** * @package multiform * @subpackage tests @@ -159,7 +159,7 @@ class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly { ); } } - + /** * @package multiform * @subpackage tests