Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed preload bug and moved facade config to Model Helper (harmonizing).

  • Loading branch information...
commit eac266a10b76447dd821d739cb9ffa786c6f7af9 1 parent bf28eba
Gabor de Mooij authored November 01, 2012
22  RedBean/Facade.php
@@ -870,14 +870,9 @@ public static function configureFacadeWithToolbox( RedBean_ToolBox $tb ) {
870 870
 		self::$redbean->setAssociationManager(self::$associationManager);
871 871
 		self::$extAssocManager = new RedBean_AssociationManager_ExtAssociationManager( self::$toolbox );
872 872
 		$helper = new RedBean_ModelHelper();
873  
-		self::$redbean->addEventListener('update', $helper );
874  
-		self::$redbean->addEventListener('open', $helper );
875  
-		self::$redbean->addEventListener('delete', $helper );
  873
+		$helper->attachEventListeners(self::$redbean);
876 874
 		self::$associationManager->addEventListener('delete', $helper );
877 875
 		self::$duplicationManager = new RedBean_DuplicationManager(self::$toolbox);
878  
-		self::$redbean->addEventListener('after_delete', $helper );
879  
-		self::$redbean->addEventListener('after_update', $helper );
880  
-		self::$redbean->addEventListener('dispense', $helper );
881 876
 		self::$tagManager = new RedBean_TagManager( self::$toolbox );
882 877
 		self::$f = new RedBean_SQLHelper(self::$adapter);
883 878
 		return $oldTools;
@@ -1158,20 +1153,7 @@ public static function getRedBean() {
1158 1153
 	 * @param array $types types to load
1159 1154
 	 */
1160 1155
 	public static function preload($beans,$types) {
1161  
-		foreach($types as $key => $type) {
1162  
-			$field = (is_numeric($key)) ? $type : $key;
1163  
-			$ids = array();
1164  
-			foreach($beans as $bean) {
1165  
-				$id = $bean->{$field.'_id'};
1166  
-				$ids[] = $id;
1167  
-				$map[$id] = $bean;
1168  
-			}	
1169  
-			$parents = R::batch($type,$ids);
1170  
-			foreach($parents as $parent) {
1171  
-				$map[$parent->id]->setProperty($field,$parent);
1172  
-			}
1173  
-			
1174  
-		}
  1156
+		return self::$redbean->preload($beans,$types);
1175 1157
 	}
1176 1158
 	
1177 1159
 }
17  RedBean/ModelHelper.php
@@ -112,4 +112,21 @@ public static function clearDependencyInjector() {
112 112
 		self::$dependencyInjector = null;
113 113
 	}
114 114
 	
  115
+	/**
  116
+	 * Attaches the FUSE event listeners. Now the Model Helper will listen for
  117
+	 * CRUD events. If a CRUD event occurs it will send a signal to the model
  118
+	 * that belongs to the CRUD bean and this model will take over control from
  119
+	 * there.
  120
+	 * 
  121
+	 * @param Observable $observable 
  122
+	 */
  123
+	public function attachEventListeners( RedBean_Observable $observable ) {
  124
+		$observable->addEventListener('update', $this );
  125
+		$observable->addEventListener('open', $this );
  126
+		$observable->addEventListener('delete', $this );
  127
+		$observable->addEventListener('after_delete', $this );
  128
+		$observable->addEventListener('after_update', $this );
  129
+		$observable->addEventListener('dispense', $this );
  130
+	}
  131
+	
115 132
 }
29  RedBean/OODB.php
@@ -878,6 +878,35 @@ public function setAssociationManager(RedBean_AssociationManager $assoc) {
878 878
 	public function setDepList($dep) {
879 879
 		$this->dep = $dep;
880 880
 	}
  881
+	
  882
+	/**
  883
+	 * Preloads certain properties for beans.
  884
+	 * Understands aliases.
  885
+	 * 
  886
+	 * Usage: $redbean->preload($books,array('coauthor'=>'author'));
  887
+	 * 
  888
+	 * @param array $beans beans
  889
+	 * @param array $types types to load
  890
+	 */
  891
+	public function preload($beans, $types) {
  892
+		foreach($types as $key => $type) {
  893
+			$map = array();
  894
+			$field = (is_numeric($key)) ? $type : $key;
  895
+			$ids = array();
  896
+			foreach($beans as $bean) {
  897
+				$id = $bean->{$field.'_id'};
  898
+				$ids[$id] = $id;
  899
+				if (!isset($map[$id])) $map[$id] = array();
  900
+				$map[$id][] = $bean;
  901
+			}
  902
+			$parents = $this->batch($type,$ids);
  903
+			foreach($parents as $parent) {
  904
+				foreach($map[$parent->id] as $childBean) {
  905
+					$childBean->setProperty($field,$parent);
  906
+				}
  907
+			}
  908
+		}
  909
+	}
881 910
 
882 911
 }
883 912
 
17  testing/RedUNIT/Base/Preloading.php
@@ -131,6 +131,23 @@ public function run() {
131 131
 		$i=0;
132 132
 		foreach($books as $book) asrt($book->collection->name,strval(++$i).'nth');
133 133
 		
  134
+		//test with multiple same parents
  135
+		R::nuke();
  136
+		$author = R::dispense('author');
  137
+		$author->setAttr('name', 'John');
  138
+		R::store($author);
  139
+		$books = R::dispense('book', 3);
  140
+		$books[0]->title = 'First book';
  141
+		$books[1]->title = 'Second book';
  142
+		$books[2]->title = 'Third book';
  143
+		$author->ownBook = $books;
  144
+		R::store($author);
  145
+		$collection = R::findAll('book');
  146
+		R::preload($collection, array('author'));
  147
+		R::nuke();
  148
+		foreach ($collection as $item) {
  149
+			asrt($item->author->name,'John');
  150
+		}
134 151
 		
135 152
 	}	
136 153
 	

0 notes on commit eac266a

Please sign in to comment.
Something went wrong with that request. Please try again.