relation.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. abstract class Relation
  14. {
  15. /**
  16. * @var string name of the relationship in the model_from
  17. */
  18. protected $name;
  19. /**
  20. * @var Model classname of the parent model
  21. */
  22. protected $model_from;
  23. /**
  24. * @var string classname of the related model
  25. */
  26. protected $model_to;
  27. /**
  28. * @var string primary key of parent model
  29. */
  30. protected $key_from = array('id');
  31. /**
  32. * @var string foreign key in related model
  33. */
  34. protected $key_to = array();
  35. /**
  36. * @var array where & order_by conditions for loading this relation
  37. */
  38. protected $conditions = array();
  39. /**
  40. * @var bool whether it's a single object or multiple
  41. */
  42. protected $singular = false;
  43. /**
  44. * @var bool whether saving this one's model_from should cascade to save model_to
  45. */
  46. protected $cascade_save = true;
  47. /**
  48. * @var bool whether deleting this one's model_from should cascade to delete model_to
  49. */
  50. protected $cascade_delete = false;
  51. /**
  52. * Configures the relationship
  53. *
  54. * @param string the model that initiates the relationship
  55. * @param string name of the relationship
  56. * @param array config values like model_to classname, key_from & key_to
  57. */
  58. abstract public function __construct($from, $name, array $config);
  59. /**
  60. * Should get the objects related to the given object by this relation
  61. *
  62. * @param Model
  63. * @return object|array
  64. */
  65. abstract public function get(Model $from);
  66. /**
  67. * Should get the properties as associative array with alias => property, the table alias is
  68. * given to be included with the property
  69. *
  70. * @param string
  71. * @return array
  72. */
  73. public function select($table)
  74. {
  75. $props = call_user_func(array($this->model_to, 'properties'));
  76. $i = 0;
  77. $properties = array();
  78. foreach ($props as $pk => $pv)
  79. {
  80. $properties[] = array($table.'.'.$pk, $table.'_c'.$i);
  81. $i++;
  82. }
  83. return $properties;
  84. }
  85. /**
  86. * Returns tables to join and fields to select with optional additional settings like order/where
  87. *
  88. * @param string alias for the from table
  89. * @param string alias for the to table
  90. * @return array
  91. */
  92. abstract public function join($alias_from, $rel_name, $alias_to);
  93. /**
  94. * Saves the current relationships and may cascade saving to model_to instances
  95. *
  96. * @param Model instance of model_from
  97. * @param array|Model single or multiple model instances to save
  98. * @param bool whether the model_from has been saved already
  99. * @param null|bool either uses default setting (null) or forces when true or prevents when false
  100. * @todo make abstract
  101. */
  102. abstract public function save($model_from, $model_to, $original_model_id, $parent_saved, $cascade);
  103. /**
  104. * Takes the current relations and attempts to delete them when cascading is allowed or forced
  105. *
  106. * @param Model instance of model_from
  107. * @param array|Model single or multiple model instances to delete
  108. * @param bool whether the model_from has been saved already
  109. * @param null|bool either uses default setting (null) or forces when true or prevents when false
  110. * @todo make abstract
  111. */
  112. abstract public function delete($model_from, $model_to, $parent_deleted, $cascade);
  113. /**
  114. * Allow outside access to protected properties
  115. *
  116. * @param $property
  117. */
  118. public function __get($property)
  119. {
  120. if (strncmp($property, '_', 1) == 0 or ! property_exists($this, $property))
  121. {
  122. throw new \FuelException('Invalid relation property: '.$property);
  123. }
  124. return $this->{$property};
  125. }
  126. }