PageRenderTime 62ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/connvoi/dev
PHP | 331 lines | 255 code | 44 blank | 32 comment | 15 complexity | 30204d370f644e5a059f64569122c1c6 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  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. $query->_join($join);
  94. return $query->get();
  95. }
  96. public function select_through($table)
  97. {
  98. foreach ($this->key_through_to as $to)
  99. {
  100. $properties[] = $table.'.'.$to;
  101. }
  102. foreach ($this->key_through_from as $from)
  103. {
  104. $properties[] = $table.'.'.$from;
  105. }
  106. return $properties;
  107. }
  108. public function join($alias_from, $rel_name, $alias_to_nr, $conditions = array())
  109. {
  110. $alias_to = 't'.$alias_to_nr;
  111. $models = array(
  112. $rel_name.'_through' => array(
  113. 'model' => null,
  114. 'connection' => call_user_func(array($this->model_to, 'connection')),
  115. 'table' => array($this->table_through, $alias_to.'_through'),
  116. 'primary_key' => null,
  117. 'join_type' => \Arr::get($conditions, 'join_type') ?: \Arr::get($this->conditions, 'join_type', 'left'),
  118. 'join_on' => array(),
  119. 'columns' => $this->select_through($alias_to.'_through'),
  120. 'rel_name' => $this->model_through,
  121. 'relation' => $this
  122. ),
  123. $rel_name => array(
  124. 'model' => $this->model_to,
  125. 'connection' => call_user_func(array($this->model_to, 'connection')),
  126. 'table' => array(call_user_func(array($this->model_to, 'table')), $alias_to),
  127. 'primary_key' => call_user_func(array($this->model_to, 'primary_key')),
  128. 'join_type' => \Arr::get($conditions, 'join_type') ?: \Arr::get($this->conditions, 'join_type', 'left'),
  129. 'join_on' => array(),
  130. 'columns' => $this->select($alias_to),
  131. 'rel_name' => strpos($rel_name, '.') ? substr($rel_name, strrpos($rel_name, '.') + 1) : $rel_name,
  132. 'relation' => $this,
  133. 'where' => \Arr::get($conditions, 'where', array()),
  134. 'order_by' => \Arr::get($conditions, 'order_by') ?: \Arr::get($this->conditions, 'order_by', array()),
  135. )
  136. );
  137. reset($this->key_from);
  138. foreach ($this->key_through_from as $key)
  139. {
  140. $models[$rel_name.'_through']['join_on'][] = array($alias_from.'.'.current($this->key_from), '=', $alias_to.'_through.'.$key);
  141. next($this->key_from);
  142. }
  143. reset($this->key_to);
  144. foreach ($this->key_through_to as $key)
  145. {
  146. $models[$rel_name]['join_on'][] = array($alias_to.'_through.'.$key, '=', $alias_to.'.'.current($this->key_to));
  147. next($this->key_to);
  148. }
  149. foreach (\Arr::get($this->conditions, 'where', array()) as $key => $condition)
  150. {
  151. ! is_array($condition) and $condition = array($key, '=', $condition);
  152. if ( ! $condition[0] instanceof \Fuel\Core\Database_Expression and strpos($condition[0], '.') === false)
  153. {
  154. $condition[0] = $alias_to.'.'.$condition[0];
  155. }
  156. is_string($condition[2]) and $condition[2] = \Db::quote($condition[2], $models[$rel_name]['connection']);
  157. $models[$rel_name]['join_on'][] = $condition;
  158. }
  159. return $models;
  160. }
  161. public function save($model_from, $models_to, $original_model_ids, $parent_saved, $cascade)
  162. {
  163. if ( ! $parent_saved)
  164. {
  165. return;
  166. }
  167. if ( ! is_array($models_to) and ($models_to = is_null($models_to) ? array() : $models_to) !== array())
  168. {
  169. throw new \FuelException('Assigned relationships must be an array or null, given relationship value for '.
  170. $this->name.' is invalid.');
  171. }
  172. $original_model_ids === null and $original_model_ids = array();
  173. $del_rels = $original_model_ids;
  174. foreach ($models_to as $key => $model_to)
  175. {
  176. if ( ! $model_to instanceof $this->model_to)
  177. {
  178. throw new \FuelException('Invalid Model instance added to relations in this model.');
  179. }
  180. // Save if it's a yet unsaved object
  181. if ($model_to->is_new())
  182. {
  183. $model_to->save(false);
  184. }
  185. $current_model_id = $model_to ? $model_to->implode_pk($model_to) : null;
  186. // Check if the model was already assigned, if not INSERT relationships:
  187. if ( ! in_array($current_model_id, $original_model_ids))
  188. {
  189. $ids = array();
  190. reset($this->key_from);
  191. foreach ($this->key_through_from as $pk)
  192. {
  193. $ids[$pk] = $model_from->{current($this->key_from)};
  194. next($this->key_from);
  195. }
  196. reset($this->key_to);
  197. foreach ($this->key_through_to as $pk)
  198. {
  199. $ids[$pk] = $model_to->{current($this->key_to)};
  200. next($this->key_to);
  201. }
  202. \DB::insert($this->table_through)->set($ids)->execute(call_user_func(array($model_from, 'connection')));
  203. $original_model_ids[] = $current_model_id; // prevents inserting it a second time
  204. }
  205. else
  206. {
  207. // unset current model from from array of new relations
  208. unset($del_rels[array_search($current_model_id, $original_model_ids)]);
  209. }
  210. // ensure correct pk assignment
  211. if ($key != $current_model_id)
  212. {
  213. $model_from->unfreeze();
  214. $rel = $model_from->_relate();
  215. if ( ! empty($rel[$this->name][$key]) and $rel[$this->name][$key] === $model_to)
  216. {
  217. unset($rel[$this->name][$key]);
  218. }
  219. $rel[$this->name][$current_model_id] = $model_to;
  220. $model_from->_relate($rel);
  221. $model_from->freeze();
  222. }
  223. }
  224. // If any ids are left in $del_rels they are no longer assigned, DELETE the relationships:
  225. foreach ($del_rels as $original_model_id)
  226. {
  227. $query = \DB::delete($this->table_through);
  228. reset($this->key_from);
  229. foreach ($this->key_through_from as $key)
  230. {
  231. $query->where($key, '=', $model_from->{current($this->key_from)});
  232. next($this->key_from);
  233. }
  234. $to_keys = count($this->key_to) == 1 ? array($original_model_id) : explode('][', substr($original_model_id, 1, -1));
  235. reset($to_keys);
  236. foreach ($this->key_through_to as $key)
  237. {
  238. $query->where($key, '=', current($to_keys));
  239. next($to_keys);
  240. }
  241. $query->execute(call_user_func(array($model_from, 'connection')));
  242. }
  243. $cascade = is_null($cascade) ? $this->cascade_save : (bool) $cascade;
  244. if ($cascade and ! empty($models_to))
  245. {
  246. foreach ($models_to as $m)
  247. {
  248. $m->save();
  249. }
  250. }
  251. }
  252. public function delete($model_from, $models_to, $parent_deleted, $cascade)
  253. {
  254. if ( ! $parent_deleted)
  255. {
  256. return;
  257. }
  258. // Remove relations
  259. $model_from->unfreeze();
  260. $rels = $model_from->_relate();
  261. $rels[$this->name] = array();
  262. $model_from->_relate($rels);
  263. $model_from->freeze();
  264. // Delete all relationship entries for the model_from
  265. $this->delete_related($model_from);
  266. $cascade = is_null($cascade) ? $this->cascade_delete : (bool) $cascade;
  267. if ($cascade and ! empty($model_to))
  268. {
  269. foreach ($models_to as $m)
  270. {
  271. $m->delete();
  272. }
  273. }
  274. }
  275. public function delete_related($model_from)
  276. {
  277. // Delete all relationship entries for the model_from
  278. $query = \DB::delete($this->table_through);
  279. reset($this->key_from);
  280. foreach ($this->key_through_from as $key)
  281. {
  282. $query->where($key, '=', $model_from->{current($this->key_from)});
  283. next($this->key_from);
  284. }
  285. $query->execute(call_user_func(array($model_from, 'connection')));
  286. }
  287. }