/** * Stores a bean in the database. This function takes a * RedBean_OODBBean Bean Object $bean and stores it * in the database. If the database schema is not compatible * with this bean and RedBean runs in fluid mode the schema * will be altered to store the bean correctly. * If the database schema is not compatible with this bean and * RedBean runs in frozen mode it will throw an exception. * This function returns the primary key ID of the inserted * bean. * @throws RedBean_Exception_Security $exception * @param RedBean_OODBBean $bean * @return integer $newid */ public function store(RedBean_OODBBean $bean) { $this->signal("update", $bean); $this->check($bean); //what table does it want $table = $bean->getMeta("type"); $idfield = $this->writer->getIDField($table); //does this table exist? $tables = $this->writer->getTables(); //If not, create if (!$this->isFrozen && !in_array($table, $tables)) { $this->writer->createTable($table); } $columns = $this->writer->getColumns($table); //does the table fit? $insertvalues = array(); $insertcolumns = array(); $updatevalues = array(); foreach ($bean as $p => $v) { if ($p != $idfield) { if (!$this->isFrozen) { //What kind of property are we dealing with? $typeno = $this->writer->scanType($v); //Is this property represented in the table? if (isset($columns[$p])) { //yes it is, does it still fit? $sqlt = $this->writer->code($columns[$p]); if ($typeno > $sqlt) { //no, we have to widen the database column type $this->writer->widenColumn($table, $p, $typeno); } } else { //no it is not $this->writer->addColumn($table, $p, $typeno); } } //Okay, now we are sure that the property value will fit $insertvalues[] = $v; $insertcolumns[] = $p; $updatevalues[] = array("property" => $p, "value" => $v); } } if (!$this->isFrozen && ($uniques = $bean->getMeta("buildcommand.unique"))) { foreach ($uniques as $unique) { $this->writer->addUniqueIndex($table, $unique); } } if ($bean->{$idfield}) { if (count($updatevalues) > 0) { $this->writer->updateRecord($table, $updatevalues, $bean->{$idfield}); } return (int) $bean->{$idfield}; } else { $id = $this->writer->insertRecord($table, $insertcolumns, array($insertvalues)); $bean->{$idfield} = $id; return (int) $id; } }
/** * Facilitates preloading. If you want to load multiple beans at once * these beans can be locked individually; given N beans this means approx. * N*3 queries which is quite a lot. This method allows you to pre-lock or pre-open * multiple entries at once. All beans will get an opened stamp that correspond to * the first bean opened. This means this approach is conservative; it might * produce a higher rate of false alarms but it does not compromise * concurrency security. * @param string $type * @param array $ids */ public function preLoad($type, $ids) { $this->adapter->exec("INSERT INTO __log (id,action,tbl,itemid)\n\t\tVALUES(NULL, :action,:tbl,:id)", array(":action" => 1, ":tbl" => "__no_type__", ":id" => 0)); //Write a multi opened record $insertid = $this->adapter->getInsertID(); $values = array(); foreach ($ids as $id) { //The returned Ids will be stored in a stash buffer $this->stash[$id] = $insertid; //the onEvent OPEN will empty this stash $values[] = array(1, $type, $id); //by using up the ids in it. } $this->writer->insertRecord("__log", array("action", "tbl", "itemid"), $values); //use extend. insert if possible }
/** * Facilitates preloading. If you want to load multiple beans at once * these beans can be locked individually; given N beans this means approx. * N*3 queries which is quite a lot. This method allows you to pre-lock or pre-open * multiple entries at once. All beans will get an opened stamp that correspond to * the first bean opened. This means this approach is conservative; it might * produce a higher rate of false alarms but it does not compromise * concurrency security. * @param string $type * @param array $ids */ public function preLoad($type, $ids) { $this->adapter->exec("INSERT INTO __log (id,action,tbl,itemid)\n\t\tVALUES(NULL, :action,:tbl,:id)", array(":action" => 1, ":tbl" => "__no_type__", ":id" => 0)); $insertid = $this->adapter->getInsertID(); $values = array(); foreach ($ids as $id) { $this->stash[$id] = $insertid; $values[] = array(1, $type, $id); } $this->writer->insertRecord("__log", array("action", "tbl", "itemid"), $values); }
/** * Stores a bean in the database. This function takes a * RedBean_OODBBean Bean Object $bean and stores it * in the database. If the database schema is not compatible * with this bean and RedBean runs in fluid mode the schema * will be altered to store the bean correctly. * If the database schema is not compatible with this bean and * RedBean runs in frozen mode it will throw an exception. * This function returns the primary key ID of the inserted * bean. * @throws RedBean_Exception_Security $exception * @param RedBean_OODBBean $bean * @return integer $newid */ public function store( RedBean_OODBBean $bean ) { $this->signal( "update", $bean ); if (!$this->isFrozen) $this->check($bean); $table = $bean->getMeta("type"); $idfield = $this->writer->getIDField($table); if (!$this->isFrozen && !$this->tableExists($table)) { $this->writer->createTable( $table ); } if (!$this->isFrozen) { $columns = $this->writer->getColumns($table) ; } $insertvalues = array(); $insertcolumns = array(); $updatevalues = array(); foreach( $bean as $p=>$v ) { if ($p!=$idfield) { if (!$this->isFrozen) { if ($bean->getMeta("cast.$p",-1)!==-1) { $cast = $bean->getMeta("cast.$p"); if ($cast=="string") { $typeno = $this->writer->scanType("STRING"); } else { throw new RedBean_Exception("Invalid Cast"); } } else { $typeno = $this->writer->scanType($v); } if (isset($columns[$p])) { $sqlt = $this->writer->code($columns[$p]); if ($typeno > $sqlt) { $this->writer->widenColumn( $table, $p, $typeno ); } } else { $this->writer->addColumn($table, $p, $typeno); } } $insertvalues[] = $v; $insertcolumns[] = $p; $updatevalues[] = array( "property"=>$p, "value"=>$v ); } } if (!$this->isFrozen && ($uniques = $bean->getMeta("buildcommand.unique"))) { foreach($uniques as $unique) { $this->writer->addUniqueIndex( $table, $unique ); } } if ($bean->$idfield) { if (count($updatevalues)>0) { $this->writer->updateRecord( $table, $updatevalues, $bean->$idfield ); } $bean->setMeta("tainted",false); $this->signal( "after_update", $bean ); return (int) $bean->$idfield; } else { $id = $this->writer->insertRecord( $table, $insertcolumns, array($insertvalues) ); $bean->$idfield = $id; $bean->setMeta("tainted",false); $this->signal( "after_update", $bean ); return (int) $id; } }