PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/packages/orm/classes/hasmany.php

https://bitbucket.org/sriedel/iccrm-wip
PHP | 267 lines | 217 code | 28 blank | 22 comment | 20 complexity | dbcf6c54dde0cd2570f723994984c757 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2012 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Orm;
  13. class HasMany extends Relation
  14. {
  15. public function __construct($from, $name, array $config)
  16. {
  17. $this->name = $name;
  18. $this->model_from = $from;
  19. $this->model_to = array_key_exists('model_to', $config)
  20. ? $config['model_to'] : \Inflector::get_namespace($from).'Model_'.\Inflector::classify($name);
  21. $this->key_from = array_key_exists('key_from', $config)
  22. ? (array) $config['key_from'] : $this->key_from;
  23. $this->key_to = array_key_exists('key_to', $config)
  24. ? (array) $config['key_to'] : (array) \Inflector::foreign_key($this->model_from);
  25. $this->conditions = array_key_exists('conditions', $config)
  26. ? (array) $config['conditions'] : array();
  27. $this->cascade_save = array_key_exists('cascade_save', $config)
  28. ? $config['cascade_save'] : $this->cascade_save;
  29. $this->cascade_delete = array_key_exists('cascade_delete', $config)
  30. ? $config['cascade_delete'] : $this->cascade_delete;
  31. if ( ! class_exists($this->model_to))
  32. {
  33. throw new \FuelException('Related model not found by Has_Many relation "'.$this->name.'": '.$this->model_to);
  34. }
  35. $this->model_to = get_real_class($this->model_to);
  36. }
  37. public function get(Model $from)
  38. {
  39. $query = call_user_func(array($this->model_to, 'find'));
  40. reset($this->key_to);
  41. foreach ($this->key_from as $key)
  42. {
  43. $query->where(current($this->key_to), $from->{$key});
  44. next($this->key_to);
  45. }
  46. foreach (\Arr::get($this->conditions, 'where', array()) as $key => $condition)
  47. {
  48. is_array($condition) or $condition = array($key, '=', $condition);
  49. $query->where($condition);
  50. }
  51. foreach (\Arr::get($this->conditions, 'order_by', array()) as $field => $direction)
  52. {
  53. if (is_numeric($field))
  54. {
  55. $query->order_by($direction);
  56. }
  57. else
  58. {
  59. $query->order_by($field, $direction);
  60. }
  61. }
  62. return $query->get();
  63. }
  64. public function join($alias_from, $rel_name, $alias_to_nr, $conditions = array())
  65. {
  66. $alias_to = 't'.$alias_to_nr;
  67. $model = array(
  68. 'model' => $this->model_to,
  69. 'connection' => call_user_func(array($this->model_to, 'connection')),
  70. 'table' => array(call_user_func(array($this->model_to, 'table')), $alias_to),
  71. 'primary_key' => call_user_func(array($this->model_to, 'primary_key')),
  72. 'join_type' => \Arr::get($conditions, 'join_type') ?: \Arr::get($this->conditions, 'join_type', 'left'),
  73. 'join_on' => array(),
  74. 'columns' => $this->select($alias_to),
  75. 'rel_name' => strpos($rel_name, '.') ? substr($rel_name, strrpos($rel_name, '.') + 1) : $rel_name,
  76. 'relation' => $this,
  77. 'where' => \Arr::get($conditions, 'where', array()),
  78. 'order_by' => \Arr::get($conditions, 'order_by') ?: \Arr::get($this->conditions, 'order_by', array()),
  79. );
  80. reset($this->key_to);
  81. foreach ($this->key_from as $key)
  82. {
  83. $model['join_on'][] = array($alias_from.'.'.$key, '=', $alias_to.'.'.current($this->key_to));
  84. next($this->key_to);
  85. }
  86. foreach (\Arr::get($this->conditions, 'where', array()) as $key => $condition)
  87. {
  88. ! is_array($condition) and $condition = array($key, '=', $condition);
  89. if ( ! $condition[0] instanceof \Fuel\Core\Database_Expression and strpos($condition[0], '.') === false)
  90. {
  91. $condition[0] = $alias_to.'.'.$condition[0];
  92. }
  93. is_string($condition[2]) and $condition[2] = \Db::quote($condition[2], $model['connection']);
  94. $model['join_on'][] = $condition;
  95. }
  96. return array($rel_name => $model);
  97. }
  98. public function save($model_from, $models_to, $original_model_ids, $parent_saved, $cascade)
  99. {
  100. if ( ! $parent_saved)
  101. {
  102. return;
  103. }
  104. if ( ! is_array($models_to) and ($models_to = is_null($models_to) ? array() : $models_to) !== array())
  105. {
  106. throw new \FuelException('Assigned relationships must be an array or null, given relationship value for '.
  107. $this->name.' is invalid.');
  108. }
  109. $original_model_ids === null and $original_model_ids = array();
  110. foreach ($models_to as $key => $model_to)
  111. {
  112. if ( ! $model_to instanceof $this->model_to)
  113. {
  114. throw new \FuelException('Invalid Model instance added to relations in this model.');
  115. }
  116. $current_model_id = ($model_to and ! $model_to->is_new()) ? $model_to->implode_pk($model_to) : null;
  117. // Check if the model was already assigned
  118. if (($model_to and $model_to->is_new()) or ! in_array($current_model_id, $original_model_ids))
  119. {
  120. // assign this object to the new objects foreign keys
  121. reset($this->key_to);
  122. $frozen = $model_to->frozen(); // only unfreeze/refreeze when it was frozen
  123. $frozen and $model_to->unfreeze();
  124. foreach ($this->key_from as $pk)
  125. {
  126. $model_to->{current($this->key_to)} = $model_from->{$pk};
  127. next($this->key_to);
  128. }
  129. $model_to->is_new() and $model_to->save(false);
  130. $frozen and $model_to->freeze();
  131. }
  132. // check if the model_to's foreign_keys match the model_from's primary keys
  133. else
  134. {
  135. // unset current model from from array
  136. unset($original_model_ids[array_search($current_model_id, $original_model_ids)]);
  137. // check if model_to still refers to this model_from
  138. $changed = false;
  139. reset($this->key_to);
  140. foreach ($this->key_from as $pk)
  141. {
  142. if ($model_to->{current($this->key_to)} != $model_from->{$pk})
  143. {
  144. $changed = true;
  145. }
  146. next($this->key_to);
  147. }
  148. // if any of the keys changed, the relationship was broken - remove model_to from loaded objects
  149. if ($changed)
  150. {
  151. $model_from->unfreeze();
  152. $rel = $model_from->_relate();
  153. unset($rel[$this->name][$key]);
  154. $model_from->_relate($rel);
  155. $model_from->freeze();
  156. // cascading this change won't work here, save just the object with cascading switched off
  157. $model_from->save(false);
  158. }
  159. }
  160. // Fix it if key isn't an imploded PK
  161. if ($key != ($current_model_id = $model_to->implode_pk($model_to)))
  162. {
  163. $model_from->unfreeze();
  164. $rel = $model_from->_relate();
  165. if ( ! empty($rel[$this->name][$key]) and $rel[$this->name][$key] === $model_to)
  166. {
  167. unset($rel[$this->name][$key]);
  168. }
  169. $rel[$this->name][$current_model_id] = $model_to;
  170. $model_from->_relate($rel);
  171. $model_from->freeze();
  172. }
  173. }
  174. // if any original ids are left over in the array, they're no longer related - break them
  175. foreach ($original_model_ids as $original_model_id)
  176. {
  177. // if still loaded set this object's old relation's foreign keys to null
  178. if ($original_model_id and $obj = call_user_func(array($this->model_to, 'find'),
  179. count($this->key_to) == 1 ? array($original_model_id) : explode('][', substr($original_model_id, 1, -1))))
  180. {
  181. $frozen = $obj->frozen(); // only unfreeze/refreeze when it was frozen
  182. $frozen and $obj->unfreeze();
  183. foreach ($this->key_to as $fk)
  184. {
  185. $obj->{$fk} = null;
  186. }
  187. $frozen and $obj->freeze();
  188. // cascading this change won't work here, save just the object with cascading switched off
  189. $obj->save(false);
  190. }
  191. }
  192. $cascade = is_null($cascade) ? $this->cascade_save : (bool) $cascade;
  193. if ($cascade and ! empty($models_to))
  194. {
  195. foreach ($models_to as $m)
  196. {
  197. $m->save();
  198. }
  199. }
  200. }
  201. public function delete($model_from, $models_to, $parent_deleted, $cascade)
  202. {
  203. if ( ! $parent_deleted)
  204. {
  205. return;
  206. }
  207. // break current relations
  208. $model_from->unfreeze();
  209. $rels = $model_from->_relate();
  210. $rels[$this->name] = array();
  211. $model_from->_relate($rels);
  212. $model_from->freeze();
  213. foreach ($models_to as $model_to)
  214. {
  215. if ( ! $model_to->frozen())
  216. {
  217. foreach ($this->key_to as $fk)
  218. {
  219. $model_to->{$fk} = null;
  220. }
  221. }
  222. }
  223. if ( ! empty($models_to))
  224. {
  225. $cascade = is_null($cascade) ? $this->cascade_delete : (bool) $cascade;
  226. foreach ($models_to as $m)
  227. {
  228. if ($cascade)
  229. {
  230. $m->delete();
  231. }
  232. else
  233. {
  234. $m->is_changed() and $m->save();
  235. }
  236. }
  237. }
  238. }
  239. }