Permalink
Browse files

Beginning refactoring of... stuff.

  • Loading branch information...
1 parent 6524ad6 commit b51972c0556cdc9d53551824b8adcd54ea8568e1 @nateabele nateabele committed Sep 17, 2010
@@ -140,16 +140,16 @@ public static function add($name, array $config = array()) {
* @return mixed A configured instance of the connection, or an array of the configuration used.
*/
public static function get($name = null, array $options = array()) {
- static $nullAdapter;
+ static $mockAdapter;
$defaults = array('config' => false, 'autoCreate' => true);
$options += $defaults;
if ($name === false) {
- if (!$nullAdapter) {
+ if (!$mockAdapter) {
$class = Libraries::locate('data.source', 'Mock');
- $nullAdapter = new $class();
+ $mockAdapter = new $class();
}
- return $nullAdapter;
+ return $mockAdapter;
}
if (!$name) {
@@ -156,7 +156,13 @@ public function &__get($name) {
* @param string $value Property value.
* @return mixed Result.
*/
- public function __set($name, $value) {
+ public function __set($name, $value = null) {
+ if (is_array($name) && !$value) {
+ foreach ($name as $key => $value) {
+ $this->__set($key, $value);
+ }
+ return;
+ }
$this->_modified[$name] = true;
$this->_data[$name] = $value;
}
@@ -753,14 +753,14 @@ public function validates($entity, array $options = array()) {
*/
public function delete($entity, array $options = array()) {
$self = static::_object();
- $_class = $self->_classes['query'];
$params = compact('entity', 'options');
- return static::_filter(__FUNCTION__, $params, function($self, $params) use ($_class) {
- $type = 'delete';
- $entity = $params['entity'];
- $options = $params['options'] + array('model' => $self) + compact('type', 'entity');
- return $self::connection()->delete(new $_class($options), $options);
+ return static::_filter(__FUNCTION__, $params, function($self, $params) {
+ $options = $params + $params['options'] + array('model' => $self, 'type' => 'delete');
+ unset($options['options']);
+
+ $query = $self::invokeMethod('_instance', array('query', $options));
+ return $self::connection()->delete($query, $options);
});
}
@@ -780,13 +780,15 @@ public function delete($entity, array $options = array()) {
*/
public static function update($data, $conditions = array(), array $options = array()) {
$self = static::_object();
- $_class = $self->_classes['query'];
$params = compact('data', 'conditions', 'options');
- return static::_filter(__FUNCTION__, $params, function($self, $params) use ($_class) {
+ return static::_filter(__FUNCTION__, $params, function($self, $params) {
$options = $params + $params['options'] + array('model' => $self, 'type' => 'update');
unset($options['options']);
- return $self::connection()->update(new $_class($options), $options);
+ var_export($options);
+
+ $query = $self::invokeMethod('_instance', array('query', $options));
+ return $self::connection()->update($query, $options);
});
}
@@ -805,25 +807,15 @@ public static function update($data, $conditions = array(), array $options = arr
*/
public static function remove($conditions = array(), array $options = array()) {
$self = static::_object();
- $_class = $self->_classes['query'];
$params = compact('conditions', 'options');
- return static::_filter(__FUNCTION__, $params, function($self, $params) use ($_class) {
+ return static::_filter(__FUNCTION__, $params, function($self, $params) {
$options = $params['options'] + $params + array('model' => $self, 'type' => 'delete');
unset($options['options']);
- return $self::connection()->delete(new $_class($options), $options);
- });
- }
- /**
- * Gets just the class name portion of a fully-name-spaced class name, i.e.
- * `app\models\Post::_name()` returns `'Post'`.
- *
- * @return string
- */
- protected static function _name() {
- static $name;
- return $name ?: $name = join('', array_slice(explode("\\", get_called_class()), -1));
+ $query = $self::invokeMethod('_instance', array('query', $options));
+ return $self::connection()->delete($query, $options);
+ });
}
/**
@@ -848,10 +840,14 @@ public static function &connection() {
}
/**
- * @deprecated
+ * Gets just the class name portion of a fully-name-spaced class name, i.e.
+ * `app\models\Post::_name()` returns `'Post'`.
+ *
+ * @return string
*/
- protected static function &_connection() {
- return static::connection();
+ protected static function _name() {
+ static $name;
+ return $name ?: $name = join('', array_slice(explode("\\", get_called_class()), -1));
}
/**
@@ -979,12 +975,20 @@ protected static function _findFilters() {
} else {
$options = $params['options'];
}
- $options += compact('classes', 'model');
- $query = new $classes['query'](array('type' => 'read') + $options);
+ $options += array('type' => 'read') + compact('model');
+ $query = $self::invokeMethod('_instance', array('query', $options));
return $self::connection()->calculation('count', $query, $options);
}
);
}
+
+ /**
+ * @deprecated
+ * @see lithium\data\Model::connection()
+ */
+ protected static function &_connection() {
+ return static::connection();
+ }
}
?>
@@ -8,21 +8,11 @@
namespace lithium\data\collection;
-use \Iterator;
-use \lithium\data\Source;
-use \lithium\util\Collection;
+use lithium\data\Source;
class DocumentSet extends \lithium\data\Collection {
- /**
- * The class dependencies for `Document`.
- *
- * @var array
- */
- protected $_classes = array(
- 'entity' => 'lithium\data\entity\Document',
- 'set' => __CLASS__
- );
+ protected $_marked = array();
/**
* PHP magic method used when setting properties on the `Document` instance, i.e.
@@ -50,15 +40,15 @@ public function __set($name, $value = null) {
$key = $path[$i];
$next = $current->__get($key);
- if (!$next instanceof Document) {
+ if (!is_object($next)) {
$next = $current->_data[$key] = $this->_relation('set', $key, array());
}
$current = $next;
}
$current->__set(end($path), $value);
}
- if ($this->_isComplexType($value) && !$value instanceof Iterator) {
+ if (is_array($value)) {
$value = $this->_relation('set', $name, $value);
}
$this->_data[$name] = $value;
@@ -114,19 +104,18 @@ public function set($values) {
public function offsetGet($offset) {
$data = null;
$null = null;
+ $model = $this->_model;
if (!isset($this->_data[$offset]) && !$data = $this->_populate(null, $offset)) {
return $null;
}
- $data = $data ?: $this->_data[$offset];
-
- if (is_a($data, $this->_classes['entity'])) {
- return $data;
+ if (isset($this->_marked[$offset])) {
+ return $this->_data[$offset];
}
-
- if ($this->_isComplexType($data)) {
+ if (is_array($this->_data[$offset])) {
$this->_data[$offset] = $this->_relation('entity', $offset, $this->_data[$offset]);
}
+ $this->_marked[$offset] = true;
return $this->_data[$offset];
}
@@ -136,17 +125,16 @@ public function offsetGet($offset) {
* @return object Returns the first `Document` object instance in the collection.
*/
public function rewind() {
- $data = parent::rewind();
+ $data = parent::rewind() ?: $this->_populate();
$key = key($this->_data);
- if (is_a($data, $this->_classes['entity'])) {
+ if (is_object($data)) {
return $data;
}
- if ($this->_isComplexType($data)) {
- $this->_data[$key] = $this->_relation('entity', $key, $this->_data[$key]);
+ if (isset($this->_data[$key])) {
+ return $this->offsetGet($key);
}
- return isset($this->_data[$key]) ? $this->_data[$key] : null;
}
public function current() {
@@ -156,7 +144,7 @@ public function current() {
/**
* Returns the next document in the set, and advances the object's internal pointer. If the end
* of the set is reached, a new document will be fetched from the data source connection handle
- * (`$_handle`). If no more documents can be fetched, returns `null`.
+ * If no more documents can be fetched, returns `null`.
*
* @return object|null Returns the next document in the set, or `null`, if no more documents are
* available.
@@ -180,32 +168,6 @@ public function export(Source $dataSource, array $options = array()) {
return array_map($map, $this->_data);
}
- /**
- * Used by getter and setter methods to determine whether the value of data is a complex type
- * that should be given its own sub-object withih the `Document`.
- *
- * @param mixed $data The data to be tested. This test is used to determine if `$data` should be
- * wrapped in an instance of `Document`.
- * @return boolean Returns `false` if the value of `$data` is a scalar type or a one-dimensional
- * array of scalar values, otherwise returns `true`.
- */
- protected function _isComplexType($data) {
- if (is_object($data) && (array) $data === array()) {
- return false;
- }
- if (is_scalar($data) || !$data) {
- return false;
- }
- if (is_array($data)) {
- if (array_keys($data) === range(0, count($data) - 1)) {
- if (array_filter($data, 'is_scalar') == array_filter($data)) {
- return false;
- }
- }
- }
- return true;
- }
-
/**
* Lazy-loads a document from a query using a reference to a database adapter and a query
* result resource.
@@ -215,13 +177,15 @@ protected function _isComplexType($data) {
* @return array
*/
protected function _populate($data = null, $key = null) {
- if ($this->closed()) {
+ if ($this->closed() || !($model = $this->_model)) {
return;
}
- if (($data = $data ?: $this->_handle->result('next', $this->_result, $this)) === null) {
+ $conn = $model::connection();
+
+ if (($data = $data ?: $conn->result('next', $this->_result, $this)) === null) {
return $this->close();
}
- return $this->_data[] = $this->_relation('entity', $key, $data, array('exists' => true));
+ return $this->_data[] = $conn->cast($model, $key, $data, array('exists' => true));
}
/**
@@ -38,18 +38,8 @@ class RecordSet extends \lithium\data\Collection {
protected $_columns = array();
/**
- * Dynamic dependancies
- *
- * @var array
- */
- protected $_classes = array(
- 'entity' => '\lithium\data\entity\Record',
- 'set' => __CLASS__
- );
-
- /**
- * Initializes the record set and uses the database handle to get the column list contained in
- * the query that created this object.
+ * Initializes the record set and uses the database connection to get the column list contained
+ * in the query that created this object.
*
* @see lithium\data\collection\RecordSet::$_columns
* @return void
@@ -59,7 +49,7 @@ class RecordSet extends \lithium\data\Collection {
protected function _init() {
parent::_init();
- if ($this->_handle && $this->_result) {
+ if ($this->_result) {
$this->_columns = $this->_columnMap();
}
if ($this->_data && !$this->_index) {
@@ -178,8 +168,8 @@ public function key($full = false) {
/**
* Returns the next record in the set, and advances the object's internal pointer. If the end of
- * the set is reached, a new record will be fetched from the data source connection handle
- * (`$_handle`). If no more records can be fetched, returns `null`.
+ * the set is reached, a new record will be fetched from the data source connection handle.
+ * If no more records can be fetched, returns `null`.
*
* @return object Returns the next record in the set, or `null`, if no more records are
* available.
@@ -265,28 +255,30 @@ public function map($filter, array $options = array()) {
* Lazy-loads records from a query using a reference to a database adapter and a query
* result resource.
*
- * @param array $record
+ * @param array $data
* @param mixed $key
* @return array
*/
- protected function _populate($record = null, $key = null) {
- if ($this->closed() && !$record) {
+ protected function _populate($data = null, $key = null) {
+ if ($this->closed() && !$record || !($model = $this->_model)) {
return;
}
- $modelClass = $this->_model;
- $class = $this->_classes['entity'];
+ $conn = $model::connection();
- if (!($record = $record ?: $this->_handle->result('next', $this->_result, $this))) {
+ if (!($data = $data ?: $conn->result('next', $this->_result, $this))) {
return $this->close();
}
+ $offset = 0;
+ $recordMap = array();
- if (is_array($record)) {
- foreach ($this->_columns as $model => $fields) {
- $data = array_combine($fields, array_slice($record, 0, count($fields)));
- $record = new $class(compact('model', 'data') + array('exists' => true));
- }
+ foreach ($this->_columns as $model => $fields) {
+ $record = array_combine($fields, array_slice($data, $offset, count($fields)));
+ $recordMap[$model] = $conn->item($model, $record, array('exists' => true));
}
- if (is_array($key = $modelClass::key($record))) {
+ $record = reset($recordMap);
+ unset($recordMap[key($recordMap)]);
+
+ if (is_array($key = $model::key($recordMap))) {
$key = count($key) === 1 ? reset($key) : $key;
}
if (in_array($key, $this->_index)) {
@@ -301,7 +293,10 @@ protected function _columnMap() {
if ($this->_query && $map = $this->_query->map()) {
return $map;
}
- return $this->_handle->schema($this->_query, $this->_result, $this);
+ if (!($model = $this->_model)) {
+ return array();
+ }
+ return $model::connection()->schema($this->_query, $this->_result, $this);
}
}
Oops, something went wrong.

0 comments on commit b51972c

Please sign in to comment.