Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reworking Relationships

  • Loading branch information...
commit 4fa7aa79b46bce8afac9d3bd5e608d5eee85e7a9 1 parent 8e840fc
@Howard3 Howard3 authored nateabele committed
View
5 data/Entity.php
@@ -120,7 +120,8 @@ class Entity extends \lithium\core\Object {
* @var array
*/
protected $_autoConfig = array(
- 'classes' => 'merge', 'parent', 'schema', 'data', 'model', 'exists', 'pathKey'
+ 'classes' => 'merge', 'parent', 'schema', 'data', 'model', 'exists', 'pathKey',
+ 'relationships'
);
/**
@@ -135,7 +136,7 @@ class Entity extends \lithium\core\Object {
* @return object Record object.
*/
public function __construct(array $config = array()) {
- $defaults = array('model' => null, 'data' => array());
+ $defaults = array('model' => null, 'data' => array(), 'relationships' => array());
parent::__construct($config + $defaults);
}
View
6 data/Source.php
@@ -276,16 +276,14 @@ public function item($model, array $data = array(), array $options = array()) {
$class = $options['class'];
unset($options['class']);
-
+
foreach ($data as $key => $val) {
switch (true) {
case is_array($val) && !is_numeric(key($val)):
$data[$key] = $this->item($model, $val, $options);
break;
case is_array($val) && is_numeric(key($val)):
- foreach($val as $k => $v) {
- $data[$key][$k] = is_object($v) ? $v : $this->item($model, $v, $options);
- }
+ $data[$key] = $this->item($model, $val, array('class' => 'set') + $options);
break;
}
}
View
23 data/collection/RecordSet.php
@@ -269,11 +269,17 @@ protected function _populate($data = null, $key = null) {
}
$key = null;
- $index = false;
$recordMap = is_object($data) ? array($model => $data) : array();
if (!$recordMap) {
- list($recordMap, $key) = $this->_mapRecord($data, $key);
+ $primary = $this->_model;
+ do {
+ list($recordMapPart, $key) = $this->_mapRecord($data, $key);
+ if(!$recordMap || $recordMapPart[$primary] == $recordMap[$primary]) {
+ $recordMap += $recordMapPart;
+ }
+ } while(($data = $this->_result->next()));
+ var_dump($recordMap);exit;
} else {
$key = $model::key(reset($recordMap));
}
@@ -299,7 +305,6 @@ protected function _mapRecord($data, $key) {
$conn = $primary::connection();
foreach ($this->_columns as $model => $fields) {
-
$fieldCount = count($fields);
$record = array_combine($fields, array_slice($data, $offset, $fieldCount));
@@ -320,18 +325,19 @@ protected function _mapRecord($data, $key) {
$useArr = ($relation->type() == 'hasMany');
if ($useArr === false) {
- $modelName = $model::meta('name');
- $recordMap[$this->_model][$model::meta('name')] = $record;
+ $recordMap[$model::meta('name')] = $record;
} else {
$recordKey = $model::key($record);
- $recordMap[$this->_model][$model::meta('name')][reset($recordKey)] = $record;
+ $recordMap[$model::meta('name')][reset($recordKey)] = $record;
}
}
$offset += $fieldCount;
}
- $model = $this->_model;
- $recordMap[$model] = $conn->item($model, $recordMap[$model], array('exists' => true));
+// $model = $this->_model;
+// $recordMap[$model] = $conn->item($model, $recordMap[$model], array(
+// 'exists' => true, 'relationships' => $relationships
+// ));
return array($recordMap, $key);
}
@@ -342,7 +348,6 @@ protected function _columnMap() {
if (!($model = $this->_model)) {
return array();
}
-
if (!$joins = $this->_query->join()) {
return $model::connection()->schema($this->_query, $this->_result, $this);
}
View
4 data/model/Query.php
@@ -584,7 +584,9 @@ protected function _associate($related) {
protected function _fromRelationship($rel) {
$model = $rel->to();
- $this->_config['relationships'][$rel->fieldName()] = $model;
+ $name = $rel->name();
+ $fieldName = $rel->fieldName();
+ $this->_config['relationships'][$name] = compact('model', 'fieldName');
$constraint = $rel->constraints();
$class = get_class($this);
Please sign in to comment.
Something went wrong with that request. Please try again.