clearRelations() public method

Removes all relations for a bean. This method breaks every connection between a certain bean $bean and every other bean of type $type. Warning: this method is really fast because it uses a direct SQL query however it does not inform the models about this. If you want to notify FUSE models about deletion use a foreach-loop with unassociate() instead. (that might be slower though)
public clearRelations ( redbeanphp\OODBBean $bean, string $type ) : void
$bean redbeanphp\OODBBean reference bean
$type string type of beans that need to be unassociated
return void
Beispiel #1
0
 /**
  * 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.
  * 
  * Tag list can be either an array with tag names or a comma separated list
  * of tag names.
  *
  * @param OODBBean $bean    bean to be tagged
  * @param array|string     $tagList a list of tags
  *
  * @return array
  */
 public function tag(OODBBean $bean, $tagList = NULL)
 {
     if (is_null($tagList)) {
         $tags = $bean->sharedTag;
         $foundTags = array();
         foreach ($tags as $tag) {
             $foundTags[] = $tag->title;
         }
         return $foundTags;
     }
     $this->associationManager->clearRelations($bean, 'tag');
     $this->addTags($bean, $tagList);
     return $tagList;
 }
Beispiel #2
0
 /**
  * Tests freezing the database.
  * After freezing the database, schema modifications are no longer
  * allowed and referring to missing columns will now cause exceptions.
  * 
  * @return void
  */
 public function testFreezer()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new AssociationManager($toolbox);
     $post = $redbean->dispense('post');
     $post->title = 'title';
     $redbean->store($post);
     $page = $redbean->dispense('page');
     $page->name = 'title';
     $redbean->store($page);
     $page = $redbean->dispense("page");
     $page->name = "John's page";
     $idpage = $redbean->store($page);
     $page2 = $redbean->dispense("page");
     $page2->name = "John's second page";
     $idpage2 = $redbean->store($page2);
     $a->associate($page, $page2);
     $redbean->freeze(TRUE);
     $page = $redbean->dispense("page");
     $page->sections = 10;
     $page->name = "half a page";
     try {
         $id = $redbean->store($page);
         fail();
     } catch (SQL $e) {
         pass();
     }
     $post = $redbean->dispense("post");
     $post->title = "existing table";
     try {
         $id = $redbean->store($post);
         pass();
     } catch (SQL $e) {
         fail();
     }
     asrt(in_array("name", array_keys($writer->getColumns("page"))), TRUE);
     asrt(in_array("sections", array_keys($writer->getColumns("page"))), FALSE);
     $newtype = $redbean->dispense("newtype");
     $newtype->property = 1;
     try {
         $id = $redbean->store($newtype);
         fail();
     } catch (SQL $e) {
         pass();
     }
     $logger = R::debug(true, 1);
     // Now log and make sure no 'describe SQL' happens
     $page = $redbean->dispense("page");
     $page->name = "just another page that has been frozen...";
     $id = $redbean->store($page);
     $page = $redbean->load("page", $id);
     $page->name = "just a frozen page...";
     $redbean->store($page);
     $page2 = $redbean->dispense("page");
     $page2->name = "an associated frozen page";
     $a->associate($page, $page2);
     $a->related($page, "page");
     $a->unassociate($page, $page2);
     $a->clearRelations($page, "page");
     $items = $redbean->find("page", array(), array("1"));
     $redbean->trash($page);
     $redbean->freeze(FALSE);
     asrt(count($logger->grep("SELECT")) > 0, TRUE);
     asrt(count($logger->grep("describe")) < 1, TRUE);
     asrt(is_array($logger->getLogs()), TRUE);
     R::debug(false);
 }