Permalink
Browse files

making keyvalue work for me

  • Loading branch information...
1 parent 34f3515 commit be602ae1fa6635abc5fbbb82017815bc7c66a7b2 @siran committed Mar 11, 2012
Showing with 17 additions and 10 deletions.
  1. +17 −10 models/behaviors/keyvalue.php
@@ -21,14 +21,14 @@ class KeyvalueBehavior extends ModelBehavior {
/**
* Settings
- *
+ *
* @var mixed
*/
public $settings = array();
/**
* Default settings
- *
+ *
* @var array
*/
protected $_defaults = array(
@@ -47,19 +47,21 @@ public function setup(&$model, $config = array()) {
/**
* Returns details for named section
- *
+ *
* @var string
* @var string
* @return array
*/
public function getSection($Model, $foreignKey = null, $section = null) {
$Model->recursive = -1;
$results = $Model->find('all',
- array('conditions' => array($this->settings[$model->alias]['foreignKey'] => $foreignKey)),
+ array('conditions' => array($this->settings[$Model->alias]['foreignKey'] => $foreignKey)),
array('fields' => array('field', 'value')));
+ if (empty($results)) return array();
+
foreach($results as $result) {
- $details[] = array('field' => $result[$model->alias]['field'], 'value' => $result[$model->alias]['value']);
+ $details[] = array('field' => $result[$Model->alias]['field'], 'value' => $result[$Model->alias]['value']);
}
$detailArray = array();
@@ -73,7 +75,7 @@ public function getSection($Model, $foreignKey = null, $section = null) {
/**
* Save details for named section
- *
+ *
* @var string
* @var array
* @var string
@@ -83,15 +85,20 @@ public function saveSection($Model, $foreignKey = null, $data = null, $section =
foreach($details as $key => $value) {
$newDetail = array();
$Model->recursive = -1;
- $tmp = $this->find('first', array(
+ $tmp = $Model->find('first', array(
'conditions' => array(
- $this->settings[$model->alias]['foreignKey'] => $foreignKey,
+ $this->settings[$Model->alias]['foreignKey'] => $foreignKey,
'field' => $section . '.' . $key),
'fields' => array('id')));
- $newDetail[$Model->alias]['id'] = $tmp[$model->alias]['id'];
+ if (!empty($tmp)) {
+ $newDetail[$Model->alias]['id'] = $tmp[$Model->alias]['id'];
+ } else {
+ $Model->create();
+ }
+ $newDetail[$Model->alias][$this->settings[$Model->alias]['foreignKey']] = $foreignKey;
$newDetail[$Model->alias]['field'] = $section . '.' . $key;
$newDetail[$Model->alias]['value'] = $value;
- $this->save($newDetail);
+ $Model->save($newDetail);
}
}
}

0 comments on commit be602ae

Please sign in to comment.