Esempio n. 1
0
 /**
  * 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;
     }
 }
Esempio n. 2
0
 /**
  * Checks whether the specified table already exists in the database.
  * Not part of the Object Database interface!
  * 
  * @param string $table table name (not type!)
  * 
  * @return boolean $exists whether the given table exists within this database.
  */
 public function tableExists($table)
 {
     //does this table exist?
     $tables = $this->writer->getTables();
     return in_array($table, $tables);
 }
Esempio n. 3
0
	/**
	 * Checks whether the specified table already exists in the database.
	 * Not part of the Object Database interface!
	 * @param string $table
	 * @return boolean $exists
	 */
	public function tableExists($table) {
		
		$tables = $this->writer->getTables();
		return in_array($this->writer->getFormattedTableName($table), $tables);
	}
Esempio n. 4
0
	/**
	 * Checks whether the specified table already exists in the database.
	 * Not part of the Object Database interface!
	 * 
	 * @param string $table table name (not type!)
	 * 
	 * @return boolean $exists whether the given table exists within this database.
	 */
	public function tableExists($table) {
		$tables = $this->writer->getTables();
		return in_array(($table), $tables);
	}