Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: core/model/SQLMap.php
- ===================================================================
- --- core/model/SQLMap.php (revision 77921)
- +++ core/model/SQLMap.php (working copy)
- @@ -40,8 +40,7 @@
- $this->query->where[] = "$baseTable.ID = $id";
- $record = $this->query->execute()->first();
- if($record) {
- - $className = $record['ClassName'];
- - $obj = new $className($record);
- + $obj = DataObject::data_factory()->instantiateDataObject($record);
- return $obj->Title;
- }
- }
- Index: core/model/DataObject.php
- ===================================================================
- --- core/model/DataObject.php (revision 77921)
- +++ core/model/DataObject.php (working copy)
- @@ -459,6 +459,39 @@
- return _t($this->class.'.PLURALNAME', $name);
- }
- +
- + /**
- + * Gets the {@link DataFactory} used to create DataObject records.
- + * By default returns a {@link SimpleDataFactory}, but this can be replaced by calling
- + * {@link set_data_factory()}
- + *
- + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
- + * DataObject at your own risk!
- + */
- + static function data_factory() {
- + if(!self::$data_factory) self::$data_factory = new SimpleDataFactory();
- + return self::$data_factory;
- + }
- +
- + /**
- + * Sets the {@link DataFactory} used to create DataObject records.
- + * Call this to replace the {@link SimpleDataFactory} with something more sophisticated,
- + * for example, an identity map.
- + *
- + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
- + * DataObject at your own risk!
- + */
- + static function set_data_factory(DataFactory $factory) {
- + self::$data_factory = $factory;
- + }
- +
- + /**
- + * The data factory.
- + * @see data_factory()
- + * @see set_data_factory()
- + */
- + private static $data_factory = null;
- +
- /**
- * Standard implementation of a title/label for a specific
- * record. Tries to find properties 'Title' or 'Name',
- @@ -2477,15 +2510,9 @@
- * @return mixed The new objects in an object of type $containerClass
- */
- function buildDataObjectSet($records, $containerClass = "DataObjectSet", $query = null, $baseClass = null) {
- + $factory = self::data_factory();
- foreach($records as $record) {
- - if(empty($record['RecordClassName'])) {
- - $record['RecordClassName'] = $record['ClassName'];
- - }
- - if(class_exists($record['RecordClassName'])) {
- - $results[] = new $record['RecordClassName']($record);
- - } else {
- - $results[] = new $baseClass($record);
- - }
- + $results[] = $factory->instantiateDataObject($record, $baseClass);
- }
- if(isset($results)) {
- @@ -2584,17 +2611,7 @@
- $record = $records->current();
- if($record) {
- - // Mid-upgrade, the database can have invalid RecordClassName values that need to be guarded against.
- - if(class_exists($record['RecordClassName'])) {
- - $record = new $record['RecordClassName']($record);
- - } else {
- - $record = new $this->class($record);
- - }
- -
- - // Rather than restrict classes at the SQL-query level, we now check once the object has been instantiated
- - // This lets us check up on weird errors where the class has been incorrectly set, and give warnings to our
- - // developers
- - return $record;
- + return self::data_factory()->instantiateDataObject($record, $this->class);
- }
- }
- Index: core/model/DataFactory.php
- ===================================================================
- --- core/model/DataFactory.php (revision 0)
- +++ core/model/DataFactory.php (revision 0)
- @@ -0,0 +1,24 @@
- +<?php
- +
- +/**
- + * DataFactory defines an interface that the ORM can use to instantiate {@Link DataObject}
- + * objects. The interface is intended to provide a place where an identity map can be
- + * incorporated into Sapphire.
- + *
- + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
- + * DataObject at your own risk!
- + */
- +interface DataFactory {
- + /**
- + * Instantiate a DataObject from the given database row.
- + *
- + * @param $databaseRow A map of the database values. It is expected that either ClassName
- + * or RecordClassName will be included in the map
- + * @param $defaultClass If ClassName or RecordClassName can't be found or doesn't contain an
- + * appropriate value, then use this class. It is optional, but instantiateDataObject() will
- + * throw an error if it's needed and hasn't been provided.
- + *
- + * @return DataObject The relevant DataObject object.
- + */
- + function instantiateDataObject($databaseRow, $defaultClass = null);
- +}
- Index: core/model/SimpleDataFactory.php
- ===================================================================
- --- core/model/SimpleDataFactory.php (revision 0)
- +++ core/model/SimpleDataFactory.php (revision 0)
- @@ -0,0 +1,27 @@
- +<?php
- +
- +/**
- + * Simple DataFactory that instantiates a new object for every single call. No identity mpa
- + * exists.
- + *
- + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
- + * DataObject at your own risk!
- + */
- +class SimpleDataFactory implements DataFactory {
- + /**
- + * Instantiate a DataObject from the given database row.
- + */
- + function instantiateDataObject($databaseRow, $defaultClass = null) {
- + if(empty($databaseRow['RecordClassName'])) {
- + $databaseRow['RecordClassName'] = $databaseRow['ClassName'];
- + }
- + if(class_exists($databaseRow['RecordClassName'])) {
- + return new $databaseRow['RecordClassName']($databaseRow);
- + } else if($defaultClass) {
- + return new $defaultClass($databaseRow);
- + } else {
- + user_error("SimpleDataFactory::instantiateDataObject(): Couldn't get class from " .
- + "database row and no default class given", E_USER_ERROR);
- + }
- + }
- +}
- \ No newline at end of file
- Index: search/SearchForm.php
- ===================================================================
- --- search/SearchForm.php (revision 77921)
- +++ search/SearchForm.php (working copy)
- @@ -231,8 +231,9 @@
- // Get records
- $records = DB::query($fullQuery);
- - foreach($records as $record)
- - $objects[] = new $record['ClassName']($record);
- + foreach($records as $record) {
- + $objects[] = DataObject::data_factory()->instantiateDataObject($record);
- + }
- if(isset($objects)) $doSet = new DataObjectSet($objects);
- else $doSet = new DataObjectSet();
- Index: search/SearchContext.php
- ===================================================================
- --- search/SearchContext.php (revision 77921)
- +++ search/SearchContext.php (working copy)
- @@ -162,8 +162,7 @@
- // use if a raw SQL query is needed
- $results = new DataObjectSet();
- foreach($query->execute() as $row) {
- - $className = $row['RecordClassName'];
- - $results->push(new $className($row));
- + $results->push(DataObject::data_factory()->instantiateDataObject($row));
- }
- return $results;
- //
Add Comment
Please, Sign In to add comment