Redbeans
Published: 13:21, Thursday 14 July 2011
Notes
What's this? See my article about Notes.
View as plain text file.
setup:
include rb.php
R::setup('mysql:host=host;dbname=dbname, user, pw);
dispense:
$new_account = R::dispense('account');
$new_account->name = 'nicolamr';
$new_account->full_name = 'Nicola Marcacci Rossi';
$new_account->import(array, [fields]);
store:
$id = R::store($new_account);
R::freeze(); for better performance
remove: R::trash($account);
associations:
R::associate(item1, item2); (also for changinc assoc)
$results = R::related(item, type[, sql, values]);
R::unassociate(item1, item2);
R::clearRelations(item, type);
n:1 relationships
just use normal:
read: R::relatedOne(bean, type);
change:
R::clearRelations(bean, type, newbean);
R::unrelated(bean1, type, sql, values);
find:
R::find(type, sqlwhereorderbylimit);
R::find(type, preparedsql, arraywithvalues)
R::findONe(type, ..);
trees:
R::attach(father, daughter);
children = R::children(father);
father = R::getParent(daughter)
copy:
copy = R::copy(bean, "list,of,associated,bean,types")
swap, batch
getAll(query)
getRow(query)
getCol(query)
getCell(query)
debug(true)
load:
$account = R::load('account', $id);
import:
$comment = R::dispense('comment');
$comment->import($_POST,"nickname,message");
R::store($comment);
fuse
to make beans models!
class Model_Comment extends RedBean_SimpleModel {
public function update() { ... }
}
methods:
update(), open(), delete();
(after_update, after_delete, dispense)
can use also with associations
model format:
class mymodelformatter implements RedBean_ImodelFormatter {
public function formatModel($model) {
return 'Model' . ucfirst(strtolower($model));
}
}
RedBean_HodelHelper::setModelFormatter(new mymodelformatter);
prefixes:
class MyBeanFormatter implements RedBean_IBeanFormatter{
public function formatBeanTable($table) {
return "cms_$table";
}
public function formatBeanID( $table ) {
return "{$table}_id"; // append table name to id. The table should not inclide the prefix.
}
}
R::$writer->setBeanFormatter(new MyBeanFormatter());
Write a Comment
* These fields are mandatory.