- All beans in own-lists will be duplicated as well
- All references to shared beans will be copied but not the shared beans themselves
- All references to parent objects (_id fields) will be copied but not the parents themselves
In most cases this is the desired scenario for copying beans.
This function uses a trail-array to prevent infinite recursion, if a recursive bean is found
(i.e. one that already has been processed) the ID of the bean will be returned.
This should not happen though.
Note:
This function does a reflectional database query so it may be slow.
Note:
this function actually passes the arguments to a protected function called
duplicate() that does all the work. This method takes care of creating a clone
of the bean to avoid the bean getting tainted (triggering saving when storing it).
/** * Makes a copy of a bean. This method makes a deep copy * of the bean.The copy will have the following features. * - All beans in own-lists will be duplicated as well * - All references to shared beans will be copied but not the shared beans themselves * - All references to parent objects (_id fields) will be copied but not the parents themselves * In most cases this is the desired scenario for copying beans. * This function uses a trail-array to prevent infinite recursion, if a recursive bean is found * (i.e. one that already has been processed) the ID of the bean will be returned. * This should not happen though. * * Note: * This function does a reflectional database query so it may be slow. * * @deprecated * This function is deprecated in favour of R::duplicate(). * This function has a confusing method signature, the R::duplicate() function * only accepts two arguments: bean and filters. * * @param OODBBean $bean bean to be copied * @param array $trail for internal usage, pass array() * @param boolean $pid for internal usage * @param array $white white list filter with bean types to duplicate * * @return array */ public static function dup($bean, $trail = array(), $pid = FALSE, $filters = array()) { self::$duplicationManager->setFilters($filters); return self::$duplicationManager->dup($bean, $trail, $pid); }
/** * Test duplication and caching. * * @return void */ public function DupAndCache() { testpack('Dup() and Cache'); $can = R::dispense('can')->setAttr('size', 3); $can->ownCoffee[] = R::dispense('coffee')->setAttr('color', 'black'); $can->sharedTag[] = R::dispense('tag')->setAttr('name', 'cool'); $can = R::load('can', R::store($can)); $d = new DuplicationManager(R::getToolBox()); $d->setCacheTables(TRUE); ob_start(); R::debug(1); $x = $d->dup($can); $queries = ob_get_contents(); R::debug(0); ob_end_clean(); $len1 = strlen($queries); asrt($len1 > 40, TRUE); asrt(isset($x->ownCoffee), TRUE); asrt(count($x->ownCoffee), 1); asrt(isset($x->sharedTag), TRUE); asrt(count($x->sharedTag), 1); $cache = $d->getSchema(); R::nuke(); $can = R::dispense('can')->setAttr('size', 3); $can->ownCoffee[] = R::dispense('coffee')->setAttr('color', 'black'); $can->sharedTag[] = R::dispense('tag')->setAttr('name', 'cool'); $can = R::load('can', R::store($can)); $d = new DuplicationManager(R::getToolBox()); /** * $cache = '{"book": { * "id": "INTEGER", * "title": "TEXT" * }, "bean": { * "id": "INTEGER", * "prop": "INTEGER" * }, "pessoa": { * "id": "INTEGER", * "nome": "TEXT", * "nome_meio": "TEXT", * "sobrenome": "TEXT", * "nascimento": "NUMERIC", * "reg_owner": "TEXT" * }, "documento": { * "id": "INTEGER", * "nome_documento": "TEXT", * "numero_documento": "TEXT", * "reg_owner": "TEXT", * "ownPessoa_id": "INTEGER" * }, "can": { * "id": "INTEGER", * "size": "INTEGER" * }, "coffee": { * "id": "INTEGER", * "color": "TEXT", * "can_id": "INTEGER" * }, "tag": { * "id": "INTEGER", * "name": "TEXT" * }, "can_tag": { * "id": "INTEGER", * "tag_id": "INTEGER", * "can_id": "INTEGER" * }}' */ $d->setTables($cache); ob_start(); R::debug(1); $x = $d->dup($can); $queries = ob_get_contents(); ob_end_clean(); R::debug(0); $len2 = strlen($queries); asrt(isset($x->ownCoffee), TRUE); asrt(count($x->ownCoffee), 1); asrt(isset($x->sharedTag), TRUE); asrt(count($x->sharedTag), 1); asrt(json_encode($cache), json_encode($d->getSchema())); asrt($len1 > $len2, TRUE); }