This repository was archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathKeyvalueBehavior.php
More file actions
98 lines (89 loc) · 2.34 KB
/
KeyvalueBehavior.php
File metadata and controls
98 lines (89 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* Copyright 2009 - 2013, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2009 - 2013, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Utils Plugin
*
* Utils Keyvalue Behavior
*
* @package utils
* @subpackage utils.models.behaviors
*/
class KeyvalueBehavior extends ModelBehavior {
/**
* Settings
*
* @var mixed
*/
public $settings = array();
/**
* Default settings
*
* @var array
*/
protected $_defaults = array(
'foreignKey' => 'user_id');
/**
* Setup
*
* @param object AppModel
* @param array $config
*/
public function setup(&$model, $config = array()) {
$settings = array_merge($this->_defaults, $config);
$this->settings[$model->alias] = $settings;
}
/**
* 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('fields' => array('field', 'value')));
foreach($results as $result) {
$details[] = array('field' => $result[$model->alias]['field'], 'value' => $result[$model->alias]['value']);
}
$detailArray = array();
foreach ($details as $value) {
$key = preg_split('/\./', $value['field'], 2);
$detailArray[$key[0]][$key[1]] = $value['value'];
}
return ($detailArray[$section]);
}
/**
* Save details for named section
*
* @var string
* @var array
* @var string
*/
public function saveSection($Model, $foreignKey = null, $data = null, $section = null) {
foreach($data as $model => $details) {
foreach($details as $key => $value) {
$newDetail = array();
$Model->recursive = -1;
$tmp = $Model->find('first', array(
'conditions' => array(
$this->settings[$model->alias]['foreignKey'] => $foreignKey,
'field' => $section . '.' . $key),
'fields' => array('id')));
$newDetail[$Model->alias]['id'] = $tmp[$model->alias]['id'];
$newDetail[$Model->alias]['field'] = $section . '.' . $key;
$newDetail[$Model->alias]['value'] = $value;
$this->save($newDetail);
}
}
}
}