本文整理汇总了PHP中RedBean_OODBBean类的典型用法代码示例。如果您正苦于以下问题:PHP RedBean_OODBBean类的具体用法?PHP RedBean_OODBBean怎么用?PHP RedBean_OODBBean使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RedBean_OODBBean类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onEvent
/**
* 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) {
}
}
示例2: getKeys
public static function getKeys(RedBean_OODBBean $bean, $typeName)
{
$fieldName = self::getLinkField($typeName);
$id = (int) $bean->{$fieldName};
$ids = R::getCol("select id from {$typeName} where " . $bean->getMeta("type") . "_id" . " = {$bean->id}");
return $ids;
}
示例3: onEvent
/**
* Throws an exception if information in the bean has been changed
* by another process or bean. This is actually the same as journaling
* using timestamps however with timestamps you risk race conditions
* when the measurements are not fine-grained enough; with
* auto-incremented primary key ids we dont have this risk.
* @param string $event
* @param RedBean_OODBBean $item
*/
public function onEvent($event, $item)
{
$id = $item->id;
if (!(int) $id) {
$event = "open";
}
$type = $item->getMeta("type");
if ($event == "open") {
if (isset($this->stash[$id])) {
$insertid = $this->stash[$id];
unset($this->stash[$id]);
return $insertid;
}
$insertid = $this->writer->insertRecord("__log", array("action", "tbl", "itemid"), array(array(1, $type, $id)));
$item->setMeta("opened", $insertid);
}
if ($event == "update" || $event == "delete") {
if ($item->getMeta("opened")) {
$oldid = $item->getMeta("opened");
} else {
$oldid = 0;
}
$newid = $this->checkChanges($type, $id, $oldid);
$item->setMeta("opened", $newid);
}
}
示例4: onEvent
/**
* 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..
}
}
示例5: addConstraint
/**
* Ensures that given an association between
* $bean1 and $bean2,
* if one of them gets trashed the association will be
* automatically removed.
* @param RedBean_OODBBean $bean1
* @param RedBean_OODBBean $bean2
* @return boolean $addedFKS
*/
public static function addConstraint(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $dontCache = false)
{
//Fetch the toolbox
$toolbox = RedBean_Setup::getToolBox();
RedBean_CompatManager::scanDirect($toolbox, array(RedBean_CompatManager::C_SYSTEM_MYSQL => "5"));
//Create an association manager
$association = new RedBean_AssociationManager($toolbox);
$writer = $toolbox->getWriter();
$oodb = $toolbox->getRedBean();
$adapter = $toolbox->getDatabaseAdapter();
//Frozen? Then we may not alter the schema!
if ($oodb->isFrozen()) {
return false;
}
//$adapter->getDatabase()->setDebugMode(1);
$table1 = $bean1->getMeta("type");
$table2 = $bean2->getMeta("type");
$table = $association->getTable(array($table1, $table2));
$idfield1 = $writer->getIDField($bean1->getMeta("type"));
$idfield2 = $writer->getIDField($bean2->getMeta("type"));
$bean = $oodb->dispense($table);
$property1 = $bean1->getMeta("type") . "_id";
$property2 = $bean2->getMeta("type") . "_id";
if ($property1 == $property2) {
$property2 = $bean2->getMeta("type") . "2_id";
}
$table = $adapter->escape($table);
$table1 = $adapter->escape($table1);
$table2 = $adapter->escape($table2);
$property1 = $adapter->escape($property1);
$property2 = $adapter->escape($property2);
//In Cache? Then we dont need to bother
if (isset(self::$fkcache[$table])) {
return false;
}
$db = $adapter->getCell("select database()");
$fks = $adapter->getCell("\n\t\t\tSELECT count(*)\n\t\t\tFROM information_schema.KEY_COLUMN_USAGE\n\t\t\tWHERE TABLE_SCHEMA ='{$db}' AND TABLE_NAME ='{$table}' AND\n\t\t\tCONSTRAINT_NAME <>'PRIMARY' AND REFERENCED_TABLE_NAME is not null\n\t\t");
//already foreign keys added in this association table
if ($fks > 0) {
return false;
}
//add the table to the cache, so we dont have to fire the fk query all the time.
if (!$dontCache) {
self::$fkcache[$table] = true;
}
$columns = $writer->getColumns($table);
if ($writer->code($columns[$property1]) !== RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32) {
$writer->widenColumn($table, $property1, RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32);
}
if ($writer->code($columns[$property2]) !== RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32) {
$writer->widenColumn($table, $property2, RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32);
}
$sql = "\n\t\t\tALTER TABLE " . $writer->noKW($table) . "\n\t\t\tADD FOREIGN KEY({$property1}) references {$table1}(id) ON DELETE CASCADE;\n\n\t\t";
$adapter->exec($sql);
$sql = "\n\t\t\tALTER TABLE " . $writer->noKW($table) . "\n\t\t\tADD FOREIGN KEY({$property2}) references {$table2}(id) ON DELETE CASCADE\n\t\t";
$adapter->exec($sql);
return true;
}
示例6: children
/**
*
* @param RedBean_OODBBean $parent
* @return array $childObjects
*/
public function children(RedBean_OODBBean $parent)
{
try {
$ids = $this->adapter->getCol("SELECT id FROM\n\t\t\t`" . $parent->getMeta("type") . "`\n\t\t\tWHERE `" . $this->property . "` = " . intval($parent->id) . "\n\t\t");
} catch (RedBean_Exception_SQL $e) {
return array();
}
return $this->oodb->batch($parent->getMeta("type"), $ids);
}
示例7: getKeys
public static function getKeys(RedBean_OODBBean $bean, $typeName, $name = null)
{
$fieldName = self::getLinkField($typeName, $name);
$id = (int) $bean->{$fieldName};
$columnPrefix = self::resolveColumnPrefix($name);
$columnName = $columnPrefix . $bean->getMeta("type") . '_id';
$ids = ZurmoRedBean::getCol("select id from {$typeName} where " . $columnName . " = {$bean->id}");
return $ids;
}
示例8: boxIfModel
private function boxIfModel(\RedBean_OODBBean $bean)
{
$model = $bean->box();
if (!$model instanceof Model) {
return $bean;
}
$model->bindApp($this->app);
return $model;
}
示例9: children
/**
* Returns all the nodes that have been attached to the specified
* parent node.
* @param RedBean_OODBBean $parent
* @return array $childObjects
*/
public function children(RedBean_OODBBean $parent)
{
$idfield = $this->writer->getIDField($parent->getMeta("type"));
try {
$ids = $this->writer->selectByCrit($idfield, $parent->getMeta("type"), $this->property, intval($parent->{$idfield}));
} catch (RedBean_Exception_SQL $e) {
return array();
}
return $this->oodb->batch($parent->getMeta("type"), $ids);
}
示例10: testGetIDShouldNeverPrintNotice
/**
* Tests whether getID never produces a notice.
*
* @return void
*/
public function testGetIDShouldNeverPrintNotice()
{
set_error_handler(function ($err, $errStr) {
die('>>>>FAIL :' . $err . ' ' . $errStr);
});
$bean = new RedBean_OODBBean();
$bean->getID();
restore_error_handler();
pass();
}
示例11: onEvent
/**
* 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>";
}
}
示例12: testImportFromAndTainted
/**
* Test import from and tainted.
*
* @return void
*/
public function testImportFromAndTainted()
{
testpack('Test importFrom() and Tainted');
$bean = R::dispense('bean');
R::store($bean);
$bean->name = 'abc';
asrt($bean->getMeta('tainted'), TRUE);
R::store($bean);
asrt($bean->getMeta('tainted'), FALSE);
$copy = R::dispense('bean');
R::store($copy);
$copy = R::load('bean', $copy->id);
asrt($copy->getMeta('tainted'), FALSE);
$copy->import(array('name' => 'xyz'));
asrt($copy->getMeta('tainted'), TRUE);
$copy->setMeta('tainted', FALSE);
asrt($copy->getMeta('tainted'), FALSE);
$copy->importFrom($bean);
asrt($copy->getMeta('tainted'), TRUE);
testpack('Test basic import() feature.');
$bean = new RedBean_OODBBean();
$bean->import(array("a" => 1, "b" => 2));
asrt($bean->a, 1);
asrt($bean->b, 2);
$bean->import(array("a" => 3, "b" => 4), "a,b");
asrt($bean->a, 3);
asrt($bean->b, 4);
$bean->import(array("a" => 5, "b" => 6), " a , b ");
asrt($bean->a, 5);
asrt($bean->b, 6);
$bean->import(array("a" => 1, "b" => 2));
testpack('Test inject() feature.');
$coffee = R::dispense('coffee');
$coffee->id = 2;
$coffee->liquid = 'black';
$cup = R::dispense('cup');
$cup->color = 'green';
// Pour coffee in cup
$cup->inject($coffee);
// Do we still have our own property?
asrt($cup->color, 'green');
// Did we pour the liquid in the cup?
asrt($cup->liquid, 'black');
// Id should not be transferred
asrt($cup->id, 0);
}
示例13: getAlias
/**
* Returns the alias for a type
*
* @param $type aliased type
*
* @return string $type type
*/
public function getAlias($type)
{
if ($t = RedBean_OODBBean::$fetchType) {
$type = $t;
RedBean_OODBBean::$fetchType = null;
}
return $type;
}
示例14: check
/**
* Checks whether a bean is valid
* @param RedBean_OODBBean $bean
*/
public function check(RedBean_OODBBean $bean)
{
//Is all meta information present?
if (!isset($bean->id) || !$bean->getMeta("type")) {
throw new RedBean_Exception_Security("Bean has incomplete Meta Information");
}
//Pattern of allowed characters
$pattern = '/[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]/';
//Does the type contain invalid characters?
if (preg_match($pattern, $bean->getMeta("type"))) {
throw new RedBean_Exception_Security("Bean Type is invalid");
}
//Are the properties and values valid?
foreach ($bean as $prop => $value) {
if (is_array($value) || is_object($value) || strlen($prop) < 1 || preg_match($pattern, $prop)) {
throw new RedBean_Exception_Security("Invalid Bean: property {$prop} ");
}
}
}
示例15: testMetaData
/**
* Test meta data methods.
*
* @return void
*/
public function testMetaData()
{
testpack('Test meta data');
$bean = new RedBean_OODBBean();
$bean->setMeta("this.is.a.custom.metaproperty", "yes");
asrt($bean->getMeta("this.is.a.custom.metaproperty"), "yes");
asrt($bean->getMeta("nonexistant"), NULL);
asrt($bean->getMeta("nonexistant", "abc"), "abc");
asrt($bean->getMeta("nonexistant.nested"), NULL);
asrt($bean->getMeta("nonexistant,nested", "abc"), "abc");
$bean->setMeta("test.two", "second");
asrt($bean->getMeta("test.two"), "second");
$bean->setMeta("another.little.property", "yes");
asrt($bean->getMeta("another.little.property"), "yes");
asrt($bean->getMeta("test.two"), "second");
// Copy Metadata
$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");
}