/** * Associates two Beans. This method will associate two beans with eachother. * You can then get one of the beans by using the related() function and * providing the other bean. You can also provide a base bean in the extra * parameter. This base bean allows you to add extra information to the association * record. Note that this is for advanced use only and the information will not * be added to one of the beans, just to the association record. * It's also possible to provide an array or JSON string as base bean. If you * pass a scalar this function will interpret the base bean as having one * property called 'extra' with the value of the scalar. * * @param RedBean_OODBBean $bean1 bean that will be part of the association * @param RedBean_OODBBean $bean2 bean that will be part of the association * @param mixed $extra bean, scalar, array or JSON providing extra data. * * @return mixed */ public static function associate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $extra = null) { //No extra? Just associate like always (default) if (!$extra) { return self::$associationManager->associate($bean1, $bean2); } else { if (!is_array($extra)) { $info = json_decode($extra, true); if (!$info) { $info = array("extra" => $extra); } } else { $info = $extra; } $bean = RedBean_Facade::dispense("typeLess"); $bean->import($info); return self::$extAssocManager->extAssociate($bean1, $bean2, $bean); } }
/** * Associates two Beans. This method will associate two beans with eachother. * You can then get one of the beans by using the related() function and * providing the other bean. You can also provide a base bean in the extra * parameter. This base bean allows you to add extra information to the association * record. Note that this is for advanced use only and the information will not * be added to one of the beans, just to the association record. * It's also possible to provide an array or JSON string as base bean. If you * pass a scalar this function will interpret the base bean as having one * property called 'extra' with the value of the scalar. * * @param RedBean_OODBBean $bean1 bean that will be part of the association * @param RedBean_OODBBean $bean2 bean that will be part of the association * @param mixed $extra bean, scalar, array or JSON providing extra data. * @param string $table specific the table name if needed * * @return mixed */ public static function associate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $extra = null, $table = null) { //No extra? Just associate like always (default) if (!$extra) { return self::$associationManager->associate($bean1, $bean2, $table); } elseif ($table != null) { throw new NotSupportedException(); } else { if (!is_array($extra)) { $info = json_decode($extra, true); if (!$info) { $info = array('extra' => $extra); } } else { $info = $extra; } $bean = RedBean_Facade::dispense('typeLess'); $bean->import($info); return self::$extAssocManager->extAssociate($bean1, $bean2, $bean); } }
$idpage = $page->id; $idpage2 = $page2->id; testpack("Test Ext. Association "); $adapter->exec("DROP TABLE IF EXISTS ad_webpage "); $adapter->exec("DROP TABLE IF EXISTS webpage "); $adapter->exec("DROP TABLE IF EXISTS ad "); $webpage = $redbean->dispense("webpage"); $webpage->title = "page with ads"; $ad = $redbean->dispense("ad"); $ad->title = "buy this!"; $top = $redbean->dispense("placement"); $top->position = "top"; $bottom = $redbean->dispense("placement"); $bottom->position = "bottom"; $ea = new RedBean_ExtAssociationManager($toolbox); $ea->extAssociate($ad, $webpage, $top); $ads = $redbean->batch("ad", $ea->related($webpage, "ad")); $adsPos = $redbean->batch("ad_webpage", $ea->related($webpage, "ad", true)); asrt(count($ads), 1); asrt(count($adsPos), 1); $theAd = array_pop($ads); $theAdPos = array_pop($adsPos); asrt($theAd->title, $ad->title); asrt($theAdPos->position, $top->position); $ad2 = $redbean->dispense("ad"); $ad2->title = "buy this too!"; $ea->extAssociate($ad2, $webpage, $bottom); $ads = $redbean->batch("ad", $ea->related($webpage, "ad", true)); asrt(count($ads), 2); testpack("Cross References"); $ids = $a->related($page, "page");