Beispiel #1
0
 /**
  * Does an optimization cycle for each UPDATE event.
  *
  * @param string				$event event
  * @param RedBean_OODBBean $bean	 bean
  *
  * @return void
  */
 public function onEvent($event, $bean)
 {
     try {
         if ($event == "update") {
             //export the bean as an array
             $arr = $bean->export();
             //print_r($arr);
             //remove the id property
             unset($arr["id"]);
             //If we are left with an empty array we might as well return
             if (count($arr) == 0) {
                 return;
             }
             //fetch table name for this bean
             //get the column names for this table
             $table = $bean->getMeta("type");
             $columns = array_keys($arr);
             //Select a random column for optimization.
             $column = $columns[array_rand($columns)];
             //get the value to be optimized
             $value = $arr[$column];
             $this->optimize($table, $column, $value);
         }
     } catch (RedBean_Exception_SQL $e) {
     }
 }
 /**
  * Does an optimization cycle for each UPDATE event
  * @param string $event
  * @param RedBean_OODBBean $bean
  */
 public function onEvent($event, $bean)
 {
     try {
         if ($event == "update") {
             $arr = $bean->export();
             unset($arr["id"]);
             if (count($arr) == 0) {
                 return;
             }
             $table = $this->adapter->escape($bean->getMeta("type"));
             $columns = array_keys($arr);
             $column = $this->adapter->escape($columns[array_rand($columns)]);
             $value = $arr[$column];
             $type = $this->writer->scanType($value);
             $fields = $this->writer->getColumns($table);
             if (!in_array($column, array_keys($fields))) {
                 return;
             }
             $typeInField = $this->writer->code($fields[$column]);
             if ($type < $typeInField) {
                 $type = $this->writer->typeno_sqltype[$type];
                 $this->adapter->exec("alter table `{$table}` add __test " . $type);
                 $this->adapter->exec("update `{$table}` set __test=`{$column}`");
                 $diff = $this->adapter->getCell("select\n\t\t\t\t\t\t\tcount(*) as df from `{$table}` where\n\t\t\t\t\t\t\tstrcmp(`{$column}`,__test) != 0");
                 if (!$diff) {
                     $this->adapter->exec("alter table `{$table}` change `{$column}` `{$column}` " . $type);
                 }
                 $this->adapter->exec("alter table `{$table}` drop __test");
             }
         }
     } catch (RedBean_Exception_SQL $e) {
         //optimizer might make mistakes, dont care..
     }
 }
Beispiel #3
0
 /**
  * Does an optimization cycle for each UPDATE event.
  * @param string $event
  * @param RedBean_OODBBean $bean
  */
 public function onEvent($event, $bean)
 {
     try {
         if ($event == "update") {
             $arr = $bean->export();
             unset($arr["id"]);
             if (count($arr) == 0) {
                 return;
             }
             $table = $this->adapter->escape($bean->getMeta("type"));
             $columns = array_keys($arr);
             //Select a random column for optimization.
             $column = $this->adapter->escape($columns[array_rand($columns)]);
             $value = $arr[$column];
             $type = $this->writer->scanType($value);
             $fields = $this->writer->getColumns($table);
             if (!in_array($column, array_keys($fields))) {
                 return;
             }
             $typeInField = $this->writer->code($fields[$column]);
             //Is the type too wide?
             if ($type < $typeInField) {
                 try {
                     @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " drop __test");
                 } catch (Exception $e) {
                 }
                 //Try to re-fit the entire column; by testing it.
                 $type = $this->writer->typeno_sqltype[$type];
                 //Add a test column.
                 @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " add __test " . $type);
                 //Copy the values and see if there are differences.
                 @$this->adapter->exec("update " . $this->writer->noKW($table) . " set __test=" . $this->writer->noKW($column) . "");
                 $rows = $this->adapter->get("select " . $this->writer->noKW($column) . " as a, __test as b from " . $this->writer->noKW($table));
                 $diff = 0;
                 foreach ($rows as $row) {
                     $diff += $row["a"] != $row["b"];
                 }
                 if (!$diff) {
                     //No differences; shrink column.
                     @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " change " . $this->writer->noKW($column) . " " . $this->writer->noKW($column) . " " . $type);
                 }
                 //Throw away test column; we don't need it anymore!
                 @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " drop __test");
             } else {
                 $this->MySQLSpecificColumns($table, $column, $fields[$column], $value);
             }
         }
     } catch (RedBean_Exception_SQL $e) {
         //optimizer might make mistakes, don't care.
         //echo $e->getMessage()."<br>";
     }
 }
 /**
  * Handles a REST PUT request.
  * Updates the bean described in the payload array in the database.
  * Returns an array formatted according to RedBeanPHP REST BeanCan
  * formatting specifications.
  *
  * Format of the payload array:
  *
  * array(
  *        'bean' => array( property => value pairs )
  * )
  *
  * @return array
  */
 private function put()
 {
     if (!isset($this->payload['bean'])) {
         return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Missing parameter \'bean\'.');
     }
     if (!is_array($this->payload['bean'])) {
         return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Parameter \'bean\' must be object/array.');
     }
     foreach ($this->payload['bean'] as $key => $value) {
         if (!is_string($key) || !is_string($value)) {
             return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Object "bean" invalid.');
         }
     }
     $this->bean->import($this->payload['bean']);
     $this->oodb->store($this->bean);
     $this->bean = $this->oodb->load($this->bean->getMeta('type'), $this->bean->id);
     return $this->resp($this->bean->export());
 }
 /**
  * Injects the properties of another bean but keeps the original ID.
  * Just like import() but keeps the original ID.
  * Chainable.
  *
  * @param RedBean_OODBBean $otherBean the bean whose properties you would like to copy
  *
  * @return RedBean_OODBBean
  */
 public function inject(RedBean_OODBBean $otherBean)
 {
     $myID = $this->properties['id'];
     $this->import($otherBean->export());
     $this->id = $myID;
     return $this;
 }
Beispiel #6
0
 /**
  * Injects the properties of another bean but keeps the original ID.
  * Just like import() but keeps the original ID.
  * Chainable.
  * 
  * @param RedBean_OODBBean $otherBean the bean whose properties you would like to copy
  * 
  * @return RedBean_OODBBean $self
  */
 public function inject(RedBean_OODBBean $otherBean)
 {
     $myID = $this->id;
     $array = $otherBean->export();
     $this->import($array);
     $this->id = $myID;
     return $this;
 }
Beispiel #7
0
 /**
  * Makes a copy of a bean. This method copies the bean and
  * adds the specified associations.
  *
  * For instance: R::copy( $book, "author,library" );
  *
  * Duplicates the $book bean and copies the association links
  * author and library as well. Note that only many-to-many
  * associations can be copied. Also note that no author or library
  * beans are copied, only the connections or references to these
  * beans.
  *
  * @param RedBean_OODBBean $bean
  * @param string $associatedBeanTypesStr
  * @return array $copiedBean
  */
 public static function copy($bean, $associatedBeanTypesStr)
 {
     $type = $bean->getMeta("type");
     $copy = R::dispense($type);
     $copy->import($bean->export());
     $copy->copyMetaFrom($bean);
     $copy->id = 0;
     R::store($copy);
     $associatedBeanTypes = explode(",", $associatedBeanTypesStr);
     foreach ($associatedBeanTypes as $associatedBeanType) {
         $assocBeans = R::related($bean, $associatedBeanType);
         foreach ($assocBeans as $assocBean) {
             R::associate($copy, $assocBean);
         }
     }
     $copy->setMeta("original", $bean);
     return $copy;
 }
Beispiel #8
0
 public function exportBean(RedBean_OODBBean $bean)
 {
     $bid = $bean->getMeta('type') . '-' . $bean->getID();
     if (isset($this->recurCheck[$bid])) {
         return null;
     }
     $this->recurCheck[$bid] = $bid;
     $export = $bean->export();
     foreach ($export as $key => $value) {
         if (strpos($key, '_id') !== false) {
             $sub = str_replace('_id', '', $key);
             $subBean = $bean->{$sub};
             if ($subBean) {
                 $export[$sub] = $this->export($subBean, false);
             }
         }
     }
     $type = $bean->getMeta('type');
     $linkField = $type . '_id';
     foreach ($this->tables as $table => $cols) {
         if (strpos($table, '_') === false) {
             if (in_array($linkField, array_keys($cols))) {
                 $field = 'own' . ucfirst($table);
                 $export[$field] = self::export($bean->{$field}, false);
             }
         }
     }
     foreach ($this->tables as $table => $cols) {
         if (strpos($table, '_') !== false) {
             $parts = explode('_', $table);
             if (is_array($parts) && in_array($type, $parts)) {
                 $other = $parts[0];
                 if ($other == $type) {
                     $other = $parts[1];
                 }
                 $field = 'shared' . ucfirst($other);
                 $export[$field] = self::export($bean->{$field}, false);
             }
         }
     }
     return $export;
 }
Beispiel #9
0
asrt($bean->getMeta("test.two"), "second");
testpack("UNIT TEST RedBean OODBBean: copyMeta");
$bean = new RedBean_OODBBean();
$bean->setMeta("meta.meta", "123");
$bean2 = new RedBean_OODBBean();
asrt($bean2->getMeta("meta.meta"), NULL);
$bean2->copyMetaFrom($bean);
asrt($bean2->getMeta("meta.meta"), "123");
testpack("UNIT TEST RedBean OODBBean: import");
$bean = new RedBean_OODBBean();
$bean->import(array("a" => 1, "b" => 2));
asrt($bean->a, 1);
asrt($bean->b, 2);
testpack("UNIT TEST RedBean OODBBean: export");
$bean->setMeta("justametaproperty", "hellothere");
$arr = $bean->export();
asrt(is_array($arr), true);
asrt(isset($arr["a"]), true);
asrt(isset($arr["b"]), true);
asrt($arr["a"], 1);
asrt($arr["b"], 2);
asrt(isset($arr["__info"]), false);
$arr = $bean->export(true);
asrt(isset($arr["__info"]), true);
asrt($arr["a"], 1);
asrt($arr["b"], 2);
//Test observer
testpack("UNIT TEST Observer Mechanism ");
$observable = new ObservableMock();
$observer = new ObserverMock();
$observable->addEventListener("event1", $observer);
 /**
  * Exports a single bean
  *
  * @param RedBean_OODBBean $bean Bean to be exported
  *
  * @return array|NULL $array Array export of bean
  */
 public function exportBean(RedBean_OODBBean $bean)
 {
     $bid = $bean->getMeta('type') . '-' . $bean->getID();
     if (isset($this->recurCheck[$bid])) {
         return NULL;
     }
     $this->recurCheck[$bid] = $bid;
     $export = $bean->export();
     foreach ($export as $key => $value) {
         $this->extractParentBean($export, $bean, $key, $value);
     }
     foreach ($this->tables as $table => $cols) {
         //get all ownProperties
         $this->extractOwnList($export, $bean, $table, $cols);
     }
     foreach ($this->tables as $table => $cols) {
         //get all sharedProperties
         $this->extractSharedList($export, $bean, $table);
     }
     return $export;
 }
 /**
  * ExportAll.
  * 
  * @return void
  */
 public function testExportAll()
 {
     testpack('Test exportAll');
     $redbean = R::$redbean;
     $bean = new RedBean_OODBBean();
     $bean->import(array("a" => 1, "b" => 2));
     $bean->setMeta("justametaproperty", "hellothere");
     $arr = $bean->export();
     asrt(is_array($arr), TRUE);
     asrt(isset($arr["a"]), TRUE);
     asrt(isset($arr["b"]), TRUE);
     asrt($arr["a"], 1);
     asrt($arr["b"], 2);
     asrt(isset($arr["__info"]), FALSE);
     $arr = $bean->export(TRUE);
     asrt(isset($arr["__info"]), TRUE);
     asrt($arr["a"], 1);
     asrt($arr["b"], 2);
     $exportBean = $redbean->dispense("abean");
     $exportBean->setMeta("metaitem.bla", 1);
     $exportedBean = $exportBean->export(TRUE);
     asrt($exportedBean["__info"]["metaitem.bla"], 1);
     asrt($exportedBean["__info"]["type"], "abean");
     // Can we determine whether a bean is empty?
     testpack('test $bean->isEmpty() function');
     $bean = R::dispense('bean');
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = 1;
     asrt($bean->isEmpty(), FALSE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = 0;
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = FALSE;
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = NULL;
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     unset($bean->property);
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     // Export bug I found
     $object = R::graph(json_decode('{"type":"bandmember","name":"Duke","ownInstrument":[{"type":"instrument","name":"Piano"}]}', TRUE));
     $a = R::exportAll($object);
     pass();
     asrt(isset($a[0]), TRUE);
     asrt((int) $a[0]['id'], 0);
     asrt($a[0]['name'], 'Duke');
     asrt($a[0]['ownInstrument'][0]['name'], 'Piano');
     R::nuke();
     $v = R::dispense('village');
     $b = R::dispense('building');
     $v->name = 'a';
     $b->name = 'b';
     $v->ownBuilding[] = $b;
     $id = R::store($v);
     $a = R::exportAll($v);
     asrt($a[0]['name'], 'a');
     asrt($a[0]['ownBuilding'][0]['name'], 'b');
     $v = R::load('village', $id);
     $b2 = R::dispense('building');
     $b2->name = 'c';
     $v->ownBuilding[] = $b2;
     $a = R::exportAll($v);
     asrt($a[0]['name'], 'a');
     asrt($a[0]['ownBuilding'][0]['name'], 'b');
     asrt(count($a[0]['ownBuilding']), 2);
     list($r1, $r2) = R::dispense('army', 2);
     $r1->name = '1';
     $r2->name = '2';
     $v->sharedArmy[] = $r2;
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 1);
     R::store($v);
     $v = R::load('village', $id);
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 1);
     asrt($a[0]['name'], 'a');
     asrt($a[0]['ownBuilding'][0]['name'], 'b');
     asrt(count($a[0]['ownBuilding']), 2);
     $v->sharedArmy[] = $r1;
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 2);
     $v = R::load('village', $id);
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 1);
     $v->sharedArmy[] = $r1;
     R::store($v);
     $v = R::load('village', $id);
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 2);
 }