Esempio n. 1
0
File: rb.php Progetto: u007/FlexiPHP
 public function insert(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 table exist? If not, create
     if (!$this->isFrozen && !$this->tableExists($table)) {
         $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 || $p == $idfield && !empty($v)) {
             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);
         }
     }
     $id = $this->writer->forceInsertRecord($table, $insertcolumns, array($insertvalues));
     $bean->{$idfield} = $id;
     $bean->setMeta("tainted", false);
     return (int) $id;
 }