Permalink
Browse files

Harmonizing APIs. Finder and AssocManager.

  • Loading branch information...
gabordemooij committed Nov 19, 2012
1 parent df08add commit 198bfc28bcfcd1b1f997540b59f7bbc134efa148
@@ -343,4 +343,88 @@ public function areRelated(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2) {
}
return (count($rows)>0);
}
+
+ /**
+ * Given an array of two beans and a property, this method
+ * swaps the value of the property.
+ * This is handy if you need to swap the priority or orderNo
+ * of an item (i.e. bug-tracking, page order).
+ *
+ * @param array $beans beans
+ * @param string $property property
+ */
+ public function swap( $beans, $property ) {
+ $bean1 = array_shift($beans);
+ $bean2 = array_shift($beans);
+ $tmp = $bean1->$property;
+ $bean1->$property = $bean2->$property;
+ $bean2->$property = $tmp;
+ $this->oodb->store($bean1);
+ $this->oodb->store($bean2);
+ }
+
+ /**
+ * Returns all the beans associated with $bean.
+ * This method will return an array containing all the beans that have
+ * been associated once with the associate() function and are still
+ * associated with the bean specified. The type parameter indicates the
+ * type of beans you are looking for. You can also pass some extra SQL and
+ * values for that SQL to filter your results after fetching the
+ * related beans.
+ *
+ * Dont try to make use of subqueries, a subquery using IN() seems to
+ * be slower than two queries!
+ *
+ * Since 3.2, you can now also pass an array of beans instead just one
+ * bean as the first parameter.
+ *
+ * @param RedBean_OODBBean|array $bean the bean you have
+ * @param string $type the type of beans you want
+ * @param string $sql SQL snippet for extra filtering
+ * @param array $val values to be inserted in SQL slots
+ *
+ * @return array $beans beans yielded by your query.
+ */
+ public function relatedSimple($bean, $type, $sql=null, $values=array()) {
+ $keys = $this->related( $bean, $type );
+ if (count($keys)==0 || !is_array($keys)) return array();
+ if (!$sql) return $this->oodb->batch($type, $keys);
+ $rows = $this->writer->selectRecord( $type, array('id'=>$keys),array($sql,$values),false );
+ return $this->oodb->convertToBeans($type,$rows);
+ }
+
+ /**
+ * Returns only single associated bean.
+ *
+ * @param RedBean_OODBBean $bean bean provided
+ * @param string $type type of bean you are searching for
+ * @param string $sql SQL for extra filtering
+ * @param array $values values to be inserted in SQL slots
+ *
+ *
+ * @return RedBean_OODBBean $bean
+ */
+ public function relatedOne( RedBean_OODBBean $bean, $type, $sql=null, $values=array() ) {
+ $beans = $this->relatedSimple($bean, $type, $sql, $values);
+ if (count($beans)==0 || !is_array($beans)) return null;
+ return reset( $beans );
+ }
+
+
+ /**
+ * The opposite of related(). Returns all the beans that are not
+ * associated with the bean provided.
+ *
+ * @param RedBean_OODBBean $bean bean provided
+ * @param string $type type of bean you are searching for
+ * @param string $sql SQL for extra filtering
+ * @param array $values values to be inserted in SQL slots
+ *
+ * @return array $beans beans
+ */
+ public function unrelated(RedBean_OODBBean $bean, $type, $sql=null, $values=array()) {
+ $keys = $this->related( $bean, $type );
+ $rows = $this->writer->selectRecord( $type, array('id'=>$keys), array($sql,$values), false, true );
+ return $this->oodb->convertToBeans($type,$rows);
+ }
}
@@ -31,4 +31,26 @@ public function extAssociate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, R
$baseBean->setMeta('type', $table );
return $this->associateBeans( $bean1, $bean2, $baseBean );
}
+
+
+ /**
+ * Deprecated
+ *
+ * @param RedBean_OODBBean $beans1 bean
+ * @param RedBean_OODBBean $beans2 bean
+ * @param mixed $extra
+ * @return mixed
+ */
+ public function extAssociateSimple( $beans1, $beans2, $extra = null) {
+ if (!is_array($extra)) {
+ $info = json_decode($extra,true);
+ if (!$info) $info = array('extra'=>$extra);
+ }
+ else {
+ $info = $extra;
+ }
+ $bean = $this->oodb->dispense('xtypeless');
+ $bean->import($info);
+ return $this->extAssociate($beans1, $beans2, $bean);
+ }
}
@@ -226,4 +226,29 @@ protected function duplicate($bean,$trail=array(),$pid=false) {
if ($pid) $copy->id = $bean->id;
return $copy;
}
+
+ /**
+ * Exports a collection of beans. Handy for XML/JSON exports with a
+ * Javascript framework like Dojo or ExtJS.
+ * What will be exported:
+ * - contents of the bean
+ * - all own bean lists (recursively)
+ * - all shared beans (not THEIR own lists)
+ *
+ * @param array|RedBean_OODBBean $beans beans to be exported
+ * @param boolean $parents also export parents
+ * @param array $filters only these types (whitelist)
+ *
+ * @return array $array exported structure
+ */
+ public function exportAll($beans,$parents=false,$filters=array()) {
+ $array = array();
+ if (!is_array($beans)) $beans = array($beans);
+ foreach($beans as $bean) {
+ $this->setFilters($filters);
+ $f = $this->dup($bean,array(),true);
+ $array[] = $f->export(false,$parents,false,$filters);
+ }
+ return $array;
+ }
}
Oops, something went wrong.

0 comments on commit 198bfc2

Please sign in to comment.