PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

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