public static function tag(RedBean_OODBBean $bean, $tagList = null) { if (is_null($tagList)) { $tags = R::related($bean, "tag"); $foundTags = array(); foreach ($tags as $tag) { $foundTags[] = $tag->title; } if (self::$flagUseLegacyTaggingAPI) { return implode(",", $foundTags); } return $foundTags; } if ($tagList !== false && !is_array($tagList)) { $tags = explode(",", (string) $tagList); } else { $tags = $tagList; } R::clearRelations($bean, "tag"); if ($tagList === false) { return; } foreach ($tags as $tag) { $t = R::findOne("tag", " title = ? ", array($tag)); if (!$t) { $t = R::dispense("tag"); $t->title = $tag; R::store($t); } R::associate($bean, $t); } }
/** * Tags a bean or returns tags associated with a bean. * If $tagList is null or omitted this method will return a * comma separated list of tags associated with the bean provided. * If $tagList is a comma separated list (string) of tags all tags will * be associated with the bean. * You may also pass an array instead of a string. * * @param RedBean_OODBBean $bean bean * @param mixed $tagList tags * * @return string $commaSepListTags */ public static function tag( RedBean_OODBBean $bean, $tagList = null ) { if (is_null($tagList)) { $tags = R::related( $bean, "tag"); $foundTags = array(); foreach($tags as $tag) { $foundTags[] = $tag->title; } return implode(",",$foundTags); } if ($tagList!==false && !is_array($tagList)) $tags = explode( ",", (string)$tagList); else $tags=$tagList; if (is_array($tags)) { foreach($tags as $tag) { if (preg_match("/\W/",$tag)) throw new RedBean_Exception("Invalid Tag. Tags may only contain alpha-numeric characters"); } } R::clearRelations( $bean, "tag" ); if ($tagList===false) return; foreach($tags as $tag) { $t = R::findOne("tag"," title = ? ",array($tag)); if (!$t) { $t = R::dispense("tag"); $t->title = $tag; R::store($t); } R::associate( $bean, $t ); } }
$t->role = 'teacher'; $s->role = 'student'; $s2->role = 'student'; $s->name = 'a'; $s2->name = 'b'; R::associate($t, $s); R::associate($t, $s2); $students = R::related($t, 'person', ' "role" = ? ORDER BY "name" ', array("student")); $s = array_shift($students); $s2 = array_shift($students); asrt($s->name, 'a'); asrt($s2->name, 'b'); $s = R::relatedOne($t, 'person', ' role = ? ORDER BY "name" ', array("student")); asrt($s->name, 'a'); //empty classroom R::clearRelations($t, 'person', $s2); $students = R::related($t, 'person', ' role = ? ORDER BY "name" ', array("student")); asrt(count($students), 1); $s = reset($students); asrt($s->name, 'b'); function getList($beans, $property) { $items = array(); foreach ($beans as $bean) { $items[] = $bean->{$property}; } sort($items); return implode(",", $items); } testpack("unrelated"); $pdo->Execute("DROP TABLE person_person");
/** * Misc Test relations... * * @return void */ public function testRelationsVariation() { $track = R::dispense('track'); $album = R::dispense('cd'); $track->name = 'a'; $track->ordernum = 1; $track2 = R::dispense('track'); $track2->ordernum = 2; $track2->name = 'b'; R::associate($album, $track); R::associate($album, $track2); $tracks = R::related($album, 'track'); $track = array_shift($tracks); $track2 = array_shift($tracks); $ab = $track->name . $track2->name; asrt($ab == 'ab' || $ab == 'ba', TRUE); $t = R::dispense('person'); $s = R::dispense('person'); $s2 = R::dispense('person'); $t->name = 'a'; $t->role = 'teacher'; $s->role = 'student'; $s2->role = 'student'; $s->name = 'a'; $s2->name = 'b'; $role = R::$writer->esc('role'); R::associate($t, $s); R::associate($t, $s2); $students = R::related($t, 'person', sprintf(' %s = ? ', $role), array("student")); $s = array_shift($students); $s2 = array_shift($students); asrt($s->name == 'a' || $s2->name == 'a', TRUE); asrt($s->name == 'b' || $s2->name == 'b', TRUE); // Empty classroom R::clearRelations($t, 'person'); R::associate($t, $s2); $students = R::related($t, 'person', sprintf(' %s = ? ', $role), array("student")); asrt(count($students), 1); $s = reset($students); asrt($s->name, 'b'); }