PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/packages/orm/classes/manymany.php

https://bitbucket.org/sriedel/iccrm-wip
PHP | 349 lines | 271 code | 46 blank | 32 comment | 16 complexity | 78f053897b69f408b14012687cddde1b 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 ManyMany extends Relation
  14. {
  15. protected $key_from = array('id');
  16. protected $key_to = array('id');
  17. /**
  18. * @var string classname of model to use as connection
  19. */
  20. protected $model_through;
  21. /**
  22. * @var string table name of table to use as connection, alternative to $model_through setting
  23. */
  24. protected $table_through;
  25. /**
  26. * @var string foreign key of from model in connection table
  27. */
  28. protected $key_through_from;
  29. /**
  30. * @var string foreign key of to model in connection table
  31. */
  32. protected $key_through_to;
  33. public function __construct($from, $name, array $config)
  34. {
  35. $this->name = $name;
  36. $this->model_from = $from;
  37. $this->model_to = array_key_exists('model_to', $config)
  38. ? $config['model_to'] : \Inflector::get_namespace($from).'Model_'.\Inflector::classify($name);
  39. $this->key_from = array_key_exists('key_from', $config)
  40. ? (array) $config['key_from'] : $this->key_from;
  41. $this->key_to = array_key_exists('key_to', $config)
  42. ? (array) $config['key_to'] : $this->key_to;
  43. $this->conditions = array_key_exists('conditions', $config)
  44. ? (array) $config['conditions'] : array();
  45. if ( ! empty($config['table_through']))
  46. {
  47. $this->table_through = $config['table_through'];
  48. }
  49. else
  50. {
  51. $table_name = array($this->model_from, $this->model_to);
  52. natcasesort($table_name);
  53. $table_name = array_merge($table_name);
  54. $this->table_through = \Inflector::tableize($table_name[0]).'_'.\Inflector::tableize($table_name[1]);
  55. }
  56. $this->key_through_from = ! empty($config['key_through_from'])
  57. ? (array) $config['key_through_from'] : (array) \Inflector::foreign_key($this->model_from);
  58. $this->key_through_to = ! empty($config['key_through_to'])
  59. ? (array) $config['key_through_to'] : (array) \Inflector::foreign_key($this->model_to);
  60. $this->cascade_save = array_key_exists('cascade_save', $config)
  61. ? $config['cascade_save'] : $this->cascade_save;
  62. $this->cascade_delete = array_key_exists('cascade_delete', $config)
  63. ? $config['cascade_delete'] : $this->cascade_delete;
  64. if ( ! class_exists($this->model_to))
  65. {
  66. throw new \FuelException('Related model not found by Many_Many relation "'.$this->name.'": '.$this->model_to);
  67. }
  68. $this->model_to = get_real_class($this->model_to);
  69. }
  70. public function get(Model $from)
  71. {
  72. // Create the query on the model_through
  73. $query = call_user_func(array($this->model_to, 'find'));
  74. // set the model_from's keys as where conditions for the model_through
  75. $join = array(
  76. 'table' => array($this->table_through, 't0_through'),
  77. 'join_type' => null,
  78. 'join_on' => array(),
  79. 'columns' => $this->select_through('t0_through')
  80. );
  81. reset($this->key_from);
  82. foreach ($this->key_through_from as $key)
  83. {
  84. $query->where('t0_through.'.$key, $from->{current($this->key_from)});
  85. next($this->key_from);
  86. }
  87. reset($this->key_to);
  88. foreach ($this->key_through_to as $key)
  89. {
  90. $join['join_on'][] = array('t0_through.'.$key, '=', 't0.'.current($this->key_to));
  91. next($this->key_to);
  92. }
  93. foreach (\Arr::get($this->conditions, 'where', array()) as $key => $condition)
  94. {
  95. is_array($condition) or $condition = array($key, '=', $condition);
  96. $query->where($condition);
  97. }
  98. foreach (\Arr::get($this->conditions, 'order_by', array()) as $field => $direction)
  99. {
  100. if (is_numeric($field))
  101. {
  102. $query->order_by($direction);
  103. }
  104. else
  105. {
  106. $query->order_by($field, $direction);
  107. }
  108. }
  109. $query->_join($join);
  110. return $query->get();
  111. }
  112. public function select_through($table)
  113. {
  114. foreach ($this->key_through_to as $to)
  115. {
  116. $properties[] = $table.'.'.$to;
  117. }
  118. foreach ($this->key_through_from as $from)
  119. {
  120. $properties[] = $table.'.'.$from;
  121. }
  122. return $properties;
  123. }
  124. public function join($alias_from, $rel_name, $alias_to_nr, $conditions = array())
  125. {
  126. $alias_to = 't'.$alias_to_nr;
  127. $models = array(
  128. $rel_name.'_through' => array(
  129. 'model' => null,
  130. 'connection' => call_user_func(array($this->model_to, 'connection')),
  131. 'table' => array($this->table_through, $alias_to.'_through'),
  132. 'primary_key' => null,
  133. 'join_type' => \Arr::get($conditions, 'join_type') ?: \Arr::get($this->conditions, 'join_type', 'left'),
  134. 'join_on' => array(),
  135. 'columns' => $this->select_through($alias_to.'_through'),
  136. 'rel_name' => $this->model_through,
  137. 'relation' => $this
  138. ),
  139. $rel_name => array(
  140. 'model' => $this->model_to,
  141. 'connection' => call_user_func(array($this->model_to, 'connection')),
  142. 'table' => array(call_user_func(array($this->model_to, 'table')), $alias_to),
  143. 'primary_key' => call_user_func(array($this->model_to, 'primary_key')),
  144. 'join_type' => \Arr::get($conditions, 'join_type') ?: \Arr::get($this->conditions, 'join_type', 'left'),
  145. 'join_on' => array(),
  146. 'columns' => $this->select($alias_to),
  147. 'rel_name' => strpos($rel_name, '.') ? substr($rel_name, strrpos($rel_name, '.') + 1) : $rel_name,
  148. 'relation' => $this,
  149. 'where' => \Arr::get($conditions, 'where', array()),
  150. 'order_by' => \Arr::get($conditions, 'order_by') ?: \Arr::get($this->conditions, 'order_by', array()),
  151. )
  152. );
  153. reset($this->key_from);
  154. foreach ($this->key_through_from as $key)
  155. {
  156. $models[$rel_name.'_through']['join_on'][] = array($alias_from.'.'.current($this->key_from), '=', $alias_to.'_through.'.$key);
  157. next($this->key_from);
  158. }
  159. reset($this->key_to);
  160. foreach ($this->key_through_to as $key)
  161. {
  162. $models[$rel_name]['join_on'][] = array($alias_to.'_through.'.$key, '=', $alias_to.'.'.current($this->key_to));
  163. next($this->key_to);
  164. }
  165. foreach (\Arr::get($this->conditions, 'where', array()) as $key => $condition)
  166. {
  167. ! is_array($condition) and $condition = array($key, '=', $condition);
  168. if ( ! $condition[0] instanceof \Fuel\Core\Database_Expression and strpos($condition[0], '.') === false)
  169. {
  170. $condition[0] = $alias_to.'.'.$condition[0];
  171. }
  172. is_string($condition[2]) and $condition[2] = \Db::quote($condition[2], $models[$rel_name]['connection']);
  173. $models[$rel_name]['join_on'][] = $condition;
  174. }
  175. return $models;
  176. }
  177. public function save($model_from, $models_to, $original_model_ids, $parent_saved, $cascade)
  178. {
  179. if ( ! $parent_saved)
  180. {
  181. return;
  182. }
  183. if ( ! is_array($models_to) and ($models_to = is_null($models_to) ? array() : $models_to) !== array())
  184. {
  185. throw new \FuelException('Assigned relationships must be an array or null, given relationship value for '.
  186. $this->name.' is invalid.');
  187. }
  188. $original_model_ids === null and $original_model_ids = array();
  189. $del_rels = $original_model_ids;
  190. foreach ($models_to as $key => $model_to)
  191. {
  192. if ( ! $model_to instanceof $this->model_to)
  193. {
  194. throw new \FuelException('Invalid Model instance added to relations in this model.');
  195. }
  196. // Save if it's a yet unsaved object
  197. if ($model_to->is_new())
  198. {
  199. $model_to->save(false);
  200. }
  201. $current_model_id = $model_to ? $model_to->implode_pk($model_to) : null;
  202. // Check if the model was already assigned, if not INSERT relationships:
  203. if ( ! in_array($current_model_id, $original_model_ids))
  204. {
  205. $ids = array();
  206. reset($this->key_from);
  207. foreach ($this->key_through_from as $pk)
  208. {
  209. $ids[$pk] = $model_from->{current($this->key_from)};
  210. next($this->key_from);
  211. }
  212. reset($this->key_to);
  213. foreach ($this->key_through_to as $pk)
  214. {
  215. $ids[$pk] = $model_to->{current($this->key_to)};
  216. next($this->key_to);
  217. }
  218. \DB::insert($this->table_through)->set($ids)->execute(call_user_func(array($model_from, 'connection')));
  219. $original_model_ids[] = $current_model_id; // prevents inserting it a second time
  220. }
  221. else
  222. {
  223. // unset current model from from array of new relations
  224. unset($del_rels[array_search($current_model_id, $original_model_ids)]);
  225. }
  226. // ensure correct pk assignment
  227. if ($key != $current_model_id)
  228. {
  229. $model_from->unfreeze();
  230. $rel = $model_from->_relate();
  231. if ( ! empty($rel[$this->name][$key]) and $rel[$this->name][$key] === $model_to)
  232. {
  233. unset($rel[$this->name][$key]);
  234. }
  235. $rel[$this->name][$current_model_id] = $model_to;
  236. $model_from->_relate($rel);
  237. $model_from->freeze();
  238. }
  239. }
  240. // If any ids are left in $del_rels they are no longer assigned, DELETE the relationships:
  241. foreach ($del_rels as $original_model_id)
  242. {
  243. $query = \DB::delete($this->table_through);
  244. reset($this->key_from);
  245. foreach ($this->key_through_from as $key)
  246. {
  247. $query->where($key, '=', $model_from->{current($this->key_from)});
  248. next($this->key_from);
  249. }
  250. $to_keys = count($this->key_to) == 1 ? array($original_model_id) : explode('][', substr($original_model_id, 1, -1));
  251. reset($to_keys);
  252. foreach ($this->key_through_to as $key)
  253. {
  254. $query->where($key, '=', current($to_keys));
  255. next($to_keys);
  256. }
  257. $query->execute(call_user_func(array($model_from, 'connection')));
  258. }
  259. $cascade = is_null($cascade) ? $this->cascade_save : (bool) $cascade;
  260. if ($cascade and ! empty($models_to))
  261. {
  262. foreach ($models_to as $m)
  263. {
  264. $m->save();
  265. }
  266. }
  267. }
  268. public function delete($model_from, $models_to, $parent_deleted, $cascade)
  269. {
  270. if ( ! $parent_deleted)
  271. {
  272. return;
  273. }
  274. // Remove relations
  275. $model_from->unfreeze();
  276. $rels = $model_from->_relate();
  277. $rels[$this->name] = array();
  278. $model_from->_relate($rels);
  279. $model_from->freeze();
  280. // Delete all relationship entries for the model_from
  281. $this->delete_related($model_from);
  282. $cascade = is_null($cascade) ? $this->cascade_delete : (bool) $cascade;
  283. if ($cascade and ! empty($model_to))
  284. {
  285. foreach ($models_to as $m)
  286. {
  287. $m->delete();
  288. }
  289. }
  290. }
  291. public function delete_related($model_from)
  292. {
  293. // Delete all relationship entries for the model_from
  294. $query = \DB::delete($this->table_through);
  295. reset($this->key_from);
  296. foreach ($this->key_through_from as $key)
  297. {
  298. $query->where($key, '=', $model_from->{current($this->key_from)});
  299. next($this->key_from);
  300. }
  301. $query->execute(call_user_func(array($model_from, 'connection')));
  302. }
  303. }