/** * 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. * * @todo extract from facade * * @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 function associate($beans1, $beans2, $extra = NULL) { if (!$extra) { return $this->associationManager->associate($beans1, $beans2); } else { return $this->extAssocManager->extAssociateSimple($beans1, $beans2, $extra); } }
/** * Test extended associations. * * @return void */ public function testExtAssoc() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); R::nuke(); $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_AssociationManager_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); }