unassociate() public method

Breaks the association between two beans. This method unassociates two beans. If the method succeeds the beans will no longer form an association. In the database this means that the association record will be removed. This method uses the OODB trash() method to remove the association links, thus giving FUSE models the opportunity to hook-in additional business logic. If the $fast parameter is set to boolean TRUE this method will remove the beans without their consent, bypassing FUSE. This can be used to improve performance.
public unassociate ( $beans1, $beans2, boolean $fast = NULL ) : void
$fast boolean if TRUE, removes the entries by query without FUSE
return void
Ejemplo n.º 1
0
 /**
  * Removes all sepcified tags from the bean. The tags specified in
  * the second parameter will no longer be associated with the bean.
  * 
  * Tag list can be either an array with tag names or a comma separated list
  * of tag names.
  *
  * @param  OODBBean $bean    tagged bean
  * @param  array|string     $tagList list of tags (names)
  *
  * @return void
  */
 public function untag($bean, $tagList)
 {
     $tags = $this->extractTagsIfNeeded($tagList);
     foreach ($tags as $tag) {
         if ($t = $this->findTagByTitle($tag)) {
             $this->associationManager->unassociate($bean, $t);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Test various.
  * Test various somewhat uncommon trash/unassociate scenarios.
  * (i.e. unassociate unrelated beans, trash non-persistant beans etc).
  * Should be handled gracefully - no output checking.
  *
  * @return void
  */
 public function testVaria2()
 {
     $toolbox = R::getToolBox();
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     $a->unassociate($book, $author1);
     $a->unassociate($book, $author2);
     pass();
     $redbean->trash($redbean->dispense("bla"));
     pass();
     $bean = $redbean->dispense("bla");
     $bean->name = 1;
     $bean->id = 2;
     $redbean->trash($bean);
     pass();
 }
Ejemplo n.º 3
0
 /**
  * (FALSE should be stored as 0 not as '')
  *
  * @return voids
  */
 public function testZeroIssue()
 {
     testpack("Zero issue");
     $toolbox = R::getToolBox();
     $redbean = $toolbox->getRedBean();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $pdo = $adapter->getDatabase();
     $pdo->Execute("DROP TABLE IF EXISTS `zero`");
     $bean = $redbean->dispense("zero");
     $bean->zero = FALSE;
     $bean->title = "bla";
     $redbean->store($bean);
     asrt(count($redbean->find("zero", array(), " zero = 0 ")), 1);
     R::store(R::dispense('hack'));
     testpack("Test RedBean Security - bean interface ");
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean = $redbean->load("page", "13; drop table hack");
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     try {
         $bean = $redbean->load("page where 1; drop table hack", 1);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean = $redbean->dispense("page");
     $evil = "; drop table hack";
     $bean->id = $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     unset($bean->id);
     $bean->name = "\"" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->name = "'" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->{$evil} = 1;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     unset($bean->{$evil});
     $bean->id = 1;
     $bean->name = "\"" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->name = "'" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->{$evil} = 1;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     try {
         $redbean->trash($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     try {
         $redbean->find("::", array(), "");
     } catch (\Exception $e) {
         pass();
     }
     $adapter->exec("drop table if exists sometable");
     testpack("Test RedBean Security - query writer");
     try {
         $writer->createTable("sometable` ( `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ; drop table hack; --");
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     testpack("Test ANSI92 issue in clearrelations");
     $pdo->Execute("DROP TABLE IF EXISTS book_group");
     $pdo->Execute("DROP TABLE IF EXISTS author_book");
     $pdo->Execute("DROP TABLE IF EXISTS book");
     $pdo->Execute("DROP TABLE IF EXISTS author");
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     set1toNAssoc($a, $book, $author1);
     set1toNAssoc($a, $book, $author2);
     pass();
     $pdo->Execute("DROP TABLE IF EXISTS book_group");
     $pdo->Execute("DROP TABLE IF EXISTS book_author");
     $pdo->Execute("DROP TABLE IF EXISTS author_book");
     $pdo->Execute("DROP TABLE IF EXISTS book");
     $pdo->Execute("DROP TABLE IF EXISTS author");
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     $a->associate($book, $author1);
     $a->associate($book, $author2);
     pass();
     testpack("Test Association Issue Group keyword (Issues 9 and 10)");
     $pdo->Execute("DROP TABLE IF EXISTS `book_group`");
     $pdo->Execute("DROP TABLE IF EXISTS `group`");
     $group = $redbean->dispense("group");
     $group->name = "mygroup";
     $redbean->store($group);
     try {
         $a->associate($group, $book);
         pass();
     } catch (SQL $e) {
         fail();
     }
     // Test issue SQL error 23000
     try {
         $a->associate($group, $book);
         pass();
     } catch (SQL $e) {
         fail();
     }
     asrt((int) $adapter->getCell("select count(*) from book_group"), 1);
     //just 1 rec!
     $pdo->Execute("DROP TABLE IF EXISTS book_group");
     $pdo->Execute("DROP TABLE IF EXISTS author_book");
     $pdo->Execute("DROP TABLE IF EXISTS book");
     $pdo->Execute("DROP TABLE IF EXISTS author");
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     $a->unassociate($book, $author1);
     $a->unassociate($book, $author2);
     pass();
     $redbean->trash($redbean->dispense("bla"));
     pass();
     $bean = $redbean->dispense("bla");
     $bean->name = 1;
     $bean->id = 2;
     $redbean->trash($bean);
     pass();
 }
Ejemplo n.º 4
0
 /**
  * Unassociates the list items in the trashcan.
  *
  * @param OODBBean $bean           bean
  * @param array            $sharedTrashcan list
  *
  * @return void
  */
 private function processSharedTrashcan($bean, $sharedTrashcan)
 {
     foreach ($sharedTrashcan as $trash) {
         $this->assocManager->unassociate($trash, $bean);
     }
 }
Ejemplo n.º 5
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);
 }