EMongoEmbeddedDocument.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * EMongoEmbeddedDocument.php
  4. *
  5. * PHP version 5.2+
  6. *
  7. * @author Dariusz Górecki <darek.krk@gmail.com>
  8. * @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
  9. * @copyright 2011 CleverIT http://www.cleverit.com.pl
  10. * @license http://www.yiiframework.com/license/ BSD license
  11. * @version 1.3
  12. * @category ext
  13. * @package ext.YiiMongoDbSuite
  14. * @since v1.0.8
  15. */
  16. /**
  17. * @since v1.0.8
  18. */
  19. abstract class EMongoEmbeddedDocument extends CModel
  20. {
  21. private static $_attributes=array();
  22. /**
  23. * CMap of embedded documents
  24. * @var CMap $_embedded
  25. * @since v1.0.8
  26. */
  27. protected $_embedded=null;
  28. /**
  29. * Cacheed values for embeddedDocuments() method vall
  30. * @var array $_embeddedConfig
  31. * @since v1.3.2
  32. */
  33. protected static $_embeddedConfig = array();
  34. /**
  35. * Hold down owner pointer (if any)
  36. *
  37. * @var EMongoEmbeddedDocument $_owner
  38. * @since v1.0.8
  39. */
  40. protected $_owner=null;
  41. /**
  42. * Constructor.
  43. * @param string $scenario name of the scenario that this model is used in.
  44. * See {@link CModel::scenario} on how scenario is used by models.
  45. * @see getScenario
  46. * @since v1.0.8
  47. */
  48. public function __construct($scenario='insert')
  49. {
  50. $this->setScenario($scenario);
  51. $this->init();
  52. $this->attachBehaviors($this->behaviors());
  53. $this->afterConstruct();
  54. $this->initEmbeddedDocuments();
  55. }
  56. /**
  57. * Initializes this model.
  58. * This method is invoked in the constructor right after {@link scenario} is set.
  59. * You may override this method to provide code that is needed to initialize the model (e.g. setting
  60. * initial property values.)
  61. * @since 1.0.8
  62. */
  63. public function init(){}
  64. /**
  65. * @since v1.0.8
  66. */
  67. protected function initEmbeddedDocuments()
  68. {
  69. if(!$this->hasEmbeddedDocuments() || !$this->beforeEmbeddedDocsInit())
  70. return false;
  71. $this->_embedded = new CMap;
  72. if(!isset(self::$_embeddedConfig[get_class($this)]))
  73. self::$_embeddedConfig[get_class($this)] = $this->embeddedDocuments();
  74. $this->afterEmbeddedDocsInit();
  75. }
  76. /**
  77. * @since v1.0.8
  78. */
  79. public function onBeforeEmbeddedDocsInit($event)
  80. {
  81. $this->raiseEvent('onBeforeEmbeddedDocsInit', $event);
  82. }
  83. /**
  84. * @since v1.0.8
  85. */
  86. public function onAfterEmbeddedDocsInit($event)
  87. {
  88. $this->raiseEvent('onAfterEmbeddedDocsInit', $event);
  89. }
  90. /**
  91. * @since v1.0.8
  92. */
  93. public function onBeforeToArray($event)
  94. {
  95. $this->raiseEvent('onBeforeToArray', $event);
  96. }
  97. /**
  98. * @since v1.0.8
  99. */
  100. public function onAfterToArray($event)
  101. {
  102. $this->raiseEvent('onAfterToArray', $event);
  103. }
  104. /**
  105. * @since v1.0.8
  106. */
  107. protected function beforeToArray()
  108. {
  109. $event = new CModelEvent($this);
  110. $this->onBeforeToArray($event);
  111. return $event->isValid;
  112. }
  113. /**
  114. * @since v1.0.8
  115. */
  116. protected function afterToArray()
  117. {
  118. $this->onAfterToArray(new CModelEvent($this));
  119. }
  120. /**
  121. * @since v1.0.8
  122. */
  123. protected function beforeEmbeddedDocsInit()
  124. {
  125. $event=new CModelEvent($this);
  126. $this->onBeforeEmbeddedDocsInit($event);
  127. return $event->isValid;
  128. }
  129. /**
  130. * @since v1.0.8
  131. */
  132. protected function afterEmbeddedDocsInit()
  133. {
  134. $this->onAfterEmbeddedDocsInit(new CModelEvent());
  135. }
  136. /**
  137. * @since v1.0.8
  138. */
  139. public function __get($name)
  140. {
  141. if($this->hasEmbeddedDocuments() && isset(self::$_embeddedConfig[get_class($this)][$name])) {
  142. // Late creation of embedded documents on first access
  143. if (is_null($this->_embedded->itemAt($name))) {
  144. $docClassName = self::$_embeddedConfig[get_class($this)][$name];
  145. $doc = new $docClassName($this->getScenario());
  146. $doc->setOwner($this);
  147. $this->_embedded->add($name, $doc);
  148. }
  149. return $this->_embedded->itemAt($name);
  150. }
  151. else
  152. return parent::__get($name);
  153. }
  154. /**
  155. * @since v1.0.8
  156. */
  157. public function __set($name, $value)
  158. {
  159. if($this->hasEmbeddedDocuments() && isset(self::$_embeddedConfig[get_class($this)][$name]))
  160. {
  161. if(is_array($value)) {
  162. // Late creation of embedded documents on first access
  163. if (is_null($this->_embedded->itemAt($name))) {
  164. $docClassName = self::$_embeddedConfig[get_class($this)][$name];
  165. $doc = new $docClassName($this->getScenario());
  166. $doc->setOwner($this);
  167. $this->_embedded->add($name, $doc);
  168. }
  169. return $this->_embedded->itemAt($name)->attributes=$value;
  170. }
  171. else if($value instanceof EMongoEmbeddedDocument)
  172. return $this->_embedded->add($name, $value);
  173. }
  174. else
  175. parent::__set($name, $value);
  176. }
  177. /**
  178. * @since v1.3.2
  179. * @see CComponent::__isset()
  180. */
  181. public function __isset($name) {
  182. if($this->hasEmbeddedDocuments() && isset(self::$_embeddedConfig[get_class($this)][$name]))
  183. {
  184. return isset($this->_embedded[$name]);
  185. }
  186. else
  187. return parent::__isset($name);
  188. }
  189. /**
  190. * @since v1.0.8
  191. */
  192. public function afterValidate()
  193. {
  194. if($this->hasEmbeddedDocuments())
  195. foreach($this->_embedded as $doc)
  196. {
  197. if(!$doc->validate())
  198. {
  199. $this->addErrors($doc->getErrors());
  200. }
  201. }
  202. $this->onAfterValidate(new CEvent($this));
  203. }
  204. /**
  205. * @since v1.0.8
  206. */
  207. public function embeddedDocuments()
  208. {
  209. return array();
  210. }
  211. /**
  212. * @since v1.0.8
  213. */
  214. public function hasEmbeddedDocuments()
  215. {
  216. if(isset(self::$_embeddedConfig[get_class($this)]))
  217. return true;
  218. return count($this->embeddedDocuments()) > 0;
  219. }
  220. /**
  221. * Returns the list of attribute names.
  222. * By default, this method returns all public properties of the class.
  223. * You may override this method to change the default.
  224. * @return array list of attribute names. Defaults to all public properties of the class.
  225. * @since v1.0.8
  226. */
  227. public function attributeNames()
  228. {
  229. $className=get_class($this);
  230. if(!isset(self::$_attributes[$className]))
  231. {
  232. $class=new ReflectionClass(get_class($this));
  233. $names=array();
  234. foreach($class->getProperties() as $property)
  235. {
  236. $name=$property->getName();
  237. if($property->isPublic() && !$property->isStatic())
  238. $names[]=$name;
  239. }
  240. if($this->hasEmbeddedDocuments())
  241. {
  242. $names = array_merge($names, array_keys(self::$_embeddedConfig[get_class($this)]));
  243. }
  244. return self::$_attributes[$className]=$names;
  245. }
  246. else
  247. return self::$_attributes[$className];
  248. }
  249. /**
  250. * Returns the given object as an associative array
  251. * Fires beforeToArray and afterToArray events
  252. * @return array an associative array of the contents of this object
  253. * @since v1.0.8
  254. */
  255. public function toArray()
  256. {
  257. if($this->beforeToArray())
  258. {
  259. $arr = $this->_toArray();
  260. $this->afterToArray();
  261. return $arr;
  262. }
  263. else
  264. return array();
  265. }
  266. /**
  267. * This method does the actual convertion to an array
  268. * Does not fire any events
  269. * @return array an associative array of the contents of this object
  270. * @since v1.3.4
  271. */
  272. protected function _toArray()
  273. {
  274. $arr = array();
  275. $class=new ReflectionClass(get_class($this));
  276. foreach($class->getProperties() as $property)
  277. {
  278. $name=$property->getName();
  279. if($property->isPublic() && !$property->isStatic())
  280. $arr[$name] = $this->$name;
  281. }
  282. if($this->hasEmbeddedDocuments())
  283. foreach($this->_embedded as $key=>$value)
  284. $arr[$key]=$value->toArray();
  285. return $arr;
  286. }
  287. /**
  288. * Return owner of this document
  289. * @return EMongoEmbeddedDocument
  290. * @since v1.0.8
  291. */
  292. public function getOwner()
  293. {
  294. if($this->_owner!==null)
  295. return $this->_owner;
  296. else
  297. return null;
  298. }
  299. /**
  300. * Set owner of this document
  301. * @param EMongoEmbeddedDocument $owner
  302. * @since v1.0.8
  303. */
  304. public function setOwner(EMongoEmbeddedDocument $owner)
  305. {
  306. $this->_owner = $owner;
  307. }
  308. /**
  309. * Override default seScenario method for populating to embedded records
  310. * @see CModel::setScenario()
  311. * @since v1.0.8
  312. */
  313. public function setScenario($value)
  314. {
  315. if($this->hasEmbeddedDocuments() && $this->_embedded !== null)
  316. {
  317. foreach($this->_embedded as $doc)
  318. $doc->setScenario($value);
  319. }
  320. parent::setScenario($value);
  321. }
  322. }