-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8821 from open-sausages/pulls/3.6/restore-dynamic…
…-field-assigment BUG Renable the ability to do dynamic assignment with DBField
- Loading branch information
Showing
6 changed files
with
179 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/** | ||
* This is a fake DB field specifically design to test dynamic value assignment. You can set `scalarValueOnly` in | ||
* the constructor. You can control whetever the field will try to do a dynamic assignment by specifing | ||
* `$dynamicAssignment` in nthe consturctor. | ||
* | ||
* If the field is set to false, it will try to do a plain assignment. This is so you can save the initial value no | ||
* matter what. If the field is set to true, it will try to do a dynamic assignment. | ||
*/ | ||
class MockDynamicAssignmentDBField extends Boolean | ||
{ | ||
|
||
private $scalarOnly; | ||
private $dynamicAssignment; | ||
|
||
/** | ||
* @param string $name | ||
* @param boolean $scalarOnly Whether our fake field should be scalar only. | ||
* @param boolean $dynamicAssignment Whether our fake field will try to do a dynamic assignment. | ||
*/ | ||
public function __construct($name = '', $scalarOnly = false, $dynamicAssignment = false) | ||
{ | ||
$this->scalarOnly = $scalarOnly; | ||
$this->dynamicAssignment = $dynamicAssignment; | ||
parent::__construct($name); | ||
} | ||
|
||
/** | ||
* If the field value and dynamicAssignment are true, we'll try to do a dynamic assignment | ||
* @param mixed $value | ||
* @return array|int|mixed | ||
*/ | ||
public function prepValueForDB($value) | ||
{ | ||
if ($value) { | ||
return $this->dynamicAssignment | ||
? array('ABS(?)' => array(1)) | ||
: 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
public function scalarValueOnly() | ||
{ | ||
return $this->scalarOnly; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* This is a fake DB field specifically design to test dynamic value assignment | ||
* @property boolean $StaticScalarOnlyField | ||
* @property boolean $DynamicScalarOnlyField | ||
* @property boolean $DynamicField | ||
* @method ManyManyList MockManyMany | ||
*/ | ||
class MockDynamicAssignmentDataObject extends DataObject implements TestOnly | ||
{ | ||
|
||
private static $db = array( | ||
// This field only emits scalar value and will save | ||
'StaticScalarOnlyField' => 'MockDynamicAssignmentDBField(1,0)', | ||
|
||
// This field tries to emit dynamic assignment but will fail because of scalar only | ||
'DynamicScalarOnlyField' => 'MockDynamicAssignmentDBField(1,1)', | ||
|
||
// This field does dynamic assignment and will pass | ||
'DynamicField' => 'MockDynamicAssignmentDBField(0,1)', | ||
); | ||
|
||
private static $many_many = array( | ||
"MockManyMany" => 'MockDynamicAssignmentDataObject' | ||
); | ||
|
||
private static $belongs_many_many = array( | ||
"MockBelongsManyMany" => 'MockDynamicAssignmentDataObject' | ||
); | ||
|
||
private static $many_many_extraFields = array( | ||
'MockManyMany' => array( | ||
// This field only emits scalar value and will save | ||
'ManyManyStaticScalarOnlyField' => 'MockDynamicAssignmentDBField(1,0)', | ||
|
||
// This field tries to emit dynamic assignment but will fail because of scalar only | ||
'ManyManyDynamicScalarOnlyField' => 'MockDynamicAssignmentDBField(1,1)', | ||
|
||
// This field does dynamic assignment and will pass | ||
'ManyManyDynamicField' => 'MockDynamicAssignmentDBField(0,1)', | ||
) | ||
); | ||
} |