This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
polymorphic.php
executable file
·107 lines (107 loc) · 3.04 KB
/
polymorphic.php
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
99
100
101
102
103
104
105
106
107
<?php
/**
* Polymorphic Behavior.
*
* Allow the model to be associated with any other model object
*
* PHP versions 4 and 5
*
* Copyright (c) 2008, Andy Dawson
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2008, Andy Dawson
* @link www.ad7six.com
* @package base
* @subpackage base.models.behaviors
* @since v 0.1
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* PolymorphicBehavior class
*
* @uses ModelBehavior
* @package base
* @subpackage base.models.behaviors
*/
class PolymorphicBehavior extends ModelBehavior {
/**
* defaultSettings property
*
* @var array
* @access protected
*/
var $_defaultSettings = array(
'classField' => 'class',
'foreignKey' => 'foreign_id'
);
/**
* setup method
*
* @param mixed $model
* @param array $config
* @return void
* @access public
*/
function setup(&$model, $config = array()) {
$this->settings[$model->alias] = am ($this->_defaultSettings, $config);
}
/**
* afterFind method
*
* @param mixed $model
* @param mixed $results
* @param bool $primary
* @access public
* @return void
*/
function afterFind (&$model, $results, $primary = false) {
extract($this->settings[$model->alias]);
if ($primary && isset($results[0][$model->alias][$classField]) && $model->recursive > 0) {
foreach ($results as $key => $result) {
$associated = array();
$class = Inflector::classify($result[$model->alias][$classField]);
$foreignId = $result[$model->alias][$foreignKey];
if ($class && $foreignId) {
$result = $result[$model->alias];
if (!isset($model->$class)) {
$model->bindModel(array('belongsTo' => array(
$class => array(
'conditions' => array($model->alias . '.' . $classField => $class),
'foreignKey' => $foreignKey
)
)));
}
$conditions = array($class . '.id' => $foreignId);
$recursive = -1;
$associated = $model->$class->find('first', compact('conditions', 'recursive'));
$name = $model->$class->find('list', compact('conditions'));
$associated[$class]['display_field'] = $name[$foreignId];
$results[$key][$class] = $associated[$class];
}
}
} elseif(isset($results[$model->alias][$classField])) {
$associated = array();
$class = $results[$model->alias][$classField];
$foreignId = $results[$model->alias][$foreignKey];
if ($class && $foreignId) {
$result = $results[$model->alias];
if (!isset($model->$class)) {
$model->bindModel(array('belongsTo' => array(
$class => array(
'conditions' => array($model->alias . '.' . $classField => $class),
'foreignKey' => $foreignKey
)
)));
}
$associated = $model->$class->find(array($class . '.id' => $foreignId), array('recursive' => -1));
$associated[$class]['display_field'] = $associated[$class][$model->$class->displayField];
$results[$class] = $associated[$class];
}
}
return $results;
}
}
?>