/** * Stores a Bean in the database. If the bean contains other beans, * these will get stored as well. * @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"); //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 != "id") { 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->id) { if (count($updatevalues) > 0) { $this->writer->updateRecord($table, $updatevalues, $bean->id); } return (int) $bean->id; } else { $id = $this->writer->insertRecord($table, $insertcolumns, array($insertvalues)); $bean->id = $id; return (int) $id; } }