本文整理汇总了PHP中RedBeanPHP\Facade::dispense方法的典型用法代码示例。如果您正苦于以下问题:PHP Facade::dispense方法的具体用法?PHP Facade::dispense怎么用?PHP Facade::dispense使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RedBeanPHP\Facade
的用法示例。
在下文中一共展示了Facade::dispense方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testTypes
/**
* Test types.
* Test how RedBeanPHP OODB and OODBBean handle type and type casts.
*
* Rules:
*
* 1. before storing a bean all types are preserved except booleans (they are converted to STRINGS '0' or '1')
* 2. after store-reload all bean property values are STRINGS or NULL
* (or ARRAYS but that's only from a user perspective because these are lazy loaded)
* 3. the ID returned by store() is an INTEGER (if possible; on 32 bit systems overflowing values will be cast to STRINGS!)
*
* After loading:
* ALL VALUES EXCEPT NULL -> STRING
* NULL -> NULL
*
* @note Why not simply return bean->id in store()? Because not every driver returns the same type:
* databases without insert_id support require a separate query or a suffix returning STRINGS, not INTEGERS.
*
* @note Why not preserve types? I.e. I store integer, why do I get back a string?
* Answer: types are handled different across database platforms, would cause overhead to inspect every value for type,
* also PHP is a dynamically typed language so types should not matter that much. Another reason: due to the nature
* of RB columns in the database might change (INT -> STRING) this would cause return types to change as well which would
* cause 'cascading errors', i.e. a column gets widened and suddenly your code would break.
*
* @note Unfortunately the 32/64-bit issue cannot be tested fully. Return-strategy store() is probably the safest
* solution.
*
* @return void
*/
public function testTypes()
{
testpack('Beans can only contain STRING and NULL after reload');
R::nuke();
$bean = R::dispense('bean');
$bean->number = 123;
$bean->float = 12.3;
$bean->bool = false;
$bean->bool2 = true;
$bean->text = 'abc';
$bean->null = null;
$bean->datetime = new \DateTime('NOW', new \DateTimeZone('Europe/Amsterdam'));
$id = R::store($bean);
asrt(is_int($id), TRUE);
asrt(is_float($bean->float), TRUE);
asrt(is_integer($bean->number), TRUE);
asrt(is_string($bean->bool), TRUE);
asrt(is_string($bean->bool2), TRUE);
asrt(is_string($bean->datetime), TRUE);
asrt(is_string($bean->text), TRUE);
asrt(is_null($bean->null), TRUE);
$bean = R::load('bean', $id);
asrt(is_string($bean->id), TRUE);
asrt(is_string($bean->float), TRUE);
asrt(is_string($bean->number), TRUE);
asrt(is_string($bean->bool), TRUE);
asrt(is_string($bean->bool2), TRUE);
asrt(is_string($bean->datetime), TRUE);
asrt(is_string($bean->text), TRUE);
asrt(is_null($bean->null), TRUE);
asrt($bean->bool, '0');
asrt($bean->bool2, '1');
}
示例2: testDump
/**
* Test dump().
*
* @return void
*/
public function testDump()
{
$beans = R::dispense('bean', 2);
$beans[0]->name = 'hello';
$beans[1]->name = 'world';
$array = R::dump($beans);
asrt(is_array($array), TRUE);
foreach ($array as $item) {
asrt(is_string($item), TRUE);
}
$beans[1]->name = 'world, and a very long string that should be shortened';
$array = R::dump($beans);
asrt(is_array($array), TRUE);
asrt(strpos($array[1], '...'), 35);
//just to get 100% test cov, we dont need to test this
dmp($beans);
pass();
//test wrong input
asrt(is_array(R::dump(NULL)), TRUE);
asrt(count(R::dump(NULL)), 0);
asrt(is_array(R::dump('')), TRUE);
asrt(count(R::dump('')), 0);
asrt(is_array(R::dump(1)), TRUE);
asrt(count(R::dump(1)), 0);
asrt(is_array(R::dump(TRUE)), TRUE);
asrt(count(R::dump(FALSE)), 0);
}
示例3: testRebuilder
/**
* Test SQLite table rebuilding.
*
* @return void
*/
public function testRebuilder()
{
$toolbox = R::getToolBox();
$adapter = $toolbox->getDatabaseAdapter();
$writer = $toolbox->getWriter();
$redbean = $toolbox->getRedBean();
$pdo = $adapter->getDatabase();
$book = R::dispense('book');
$page = R::dispense('page');
$book->xownPage[] = $page;
$id = R::store($book);
$book = R::load('book', $id);
asrt(count($book->xownPage), 1);
asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
R::trash($book);
asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
$book = R::dispense('book');
$page = R::dispense('page');
$book->xownPage[] = $page;
$id = R::store($book);
$book = R::load('book', $id);
asrt(count($book->xownPage), 1);
asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
$book->added = 2;
R::store($book);
$book->added = 'added';
R::store($book);
R::trash($book);
asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
}
示例4: testGetInsertID
/**
* Tests R::getInsertID convenience method.
*
* @return void
*/
public function testGetInsertID()
{
R::nuke();
$id = R::store(R::dispense('book'));
$id2 = R::getInsertID();
asrt($id, $id2);
}
示例5: createPackageCost
function createPackageCost($label, $cost)
{
$packageCost = R::dispense('packageCost');
$packageCost->label = $label;
$packageCost->cost = $cost;
return json_encode(R::exportAll($packageCost));
}
示例6: testInnoDBIndexLimit
/**
* Test varchar 191 condition.
*
* @return void
*/
public function testInnoDBIndexLimit()
{
R::nuke();
$book = R::dispense('book');
$book->text = 'abcd';
R::store($book);
$columns = R::inspect('book');
asrt(isset($columns['text']), TRUE);
asrt($columns['text'], 'varchar(191)');
$book = $book->fresh();
$book->text = str_repeat('x', 190);
R::store($book);
$columns = R::inspect('book');
asrt(isset($columns['text']), TRUE);
asrt($columns['text'], 'varchar(191)');
$book = $book->fresh();
$book->text = str_repeat('x', 191);
R::store($book);
$columns = R::inspect('book');
asrt(isset($columns['text']), TRUE);
asrt($columns['text'], 'varchar(191)');
$book = $book->fresh();
$book->text = str_repeat('x', 192);
R::store($book);
$columns = R::inspect('book');
asrt(isset($columns['text']), TRUE);
asrt($columns['text'], 'varchar(255)');
}
示例7: testTainted
/**
* Test tainted.
*
* @return void
*/
public function testTainted()
{
testpack('Original Tainted Tests');
$redbean = R::getRedBean();
$spoon = $redbean->dispense("spoon");
asrt($spoon->getMeta("tainted"), TRUE);
$spoon->dirty = "yes";
asrt($spoon->getMeta("tainted"), TRUE);
testpack('Tainted List test');
$note = R::dispense('note');
$note->text = 'abc';
$note->ownNote[] = R::dispense('note')->setAttr('text', 'def');
$id = R::store($note);
$note = R::load('note', $id);
asrt($note->isTainted(), FALSE);
// Shouldn't affect tainted
$note->text;
asrt($note->isTainted(), FALSE);
$note->ownNote;
asrt($note->isTainted(), TRUE);
testpack('Tainted Test Old Value');
$text = $note->old('text');
asrt($text, 'abc');
asrt($note->hasChanged('text'), FALSE);
$note->text = 'xxx';
asrt($note->hasChanged('text'), TRUE);
$text = $note->old('text');
asrt($text, 'abc');
testpack('Tainted Non-exist');
asrt($note->hasChanged('text2'), FALSE);
testpack('Misc Tainted Tests');
$bean = R::dispense('bean');
$bean->hasChanged('prop');
$bean->old('prop');
}
示例8: testForeignKeysWithSQLite
/**
* Test foreign keys with SQLite.
*
* @return void
*/
public function testForeignKeysWithSQLite()
{
$book = R::dispense('book');
$page = R::dispense('page');
$cover = R::dispense('cover');
list($g1, $g2) = R::dispense('genre', 2);
$g1->name = '1';
$g2->name = '2';
$book->ownPage = array($page);
$book->cover = $cover;
$book->sharedGenre = array($g1, $g2);
R::store($book);
$fkbook = R::getAll('pragma foreign_key_list(book)');
$fkgenre = R::getAll('pragma foreign_key_list(book_genre)');
$fkpage = R::getAll('pragma foreign_key_list(page)');
asrt($fkpage[0]['from'], 'book_id');
asrt($fkpage[0]['to'], 'id');
asrt($fkpage[0]['table'], 'book');
asrt(count($fkgenre), 2);
if ($fkgenre[0]['from'] == 'book') {
asrt($fkgenre[0]['to'], 'id');
asrt($fkgenre[0]['table'], 'book');
}
if ($fkgenre[0]['from'] == 'genre') {
asrt($fkgenre[0]['to'], 'id');
asrt($fkgenre[0]['table'], 'genre');
}
asrt($fkbook[0]['from'], 'cover_id');
asrt($fkbook[0]['to'], 'id');
asrt($fkbook[0]['table'], 'cover');
}
示例9: testBasicBox
/**
* Test boxing.
*
* @return void
*/
public function testBasicBox()
{
$soup = R::dispense('soup');
$soup->flavour = 'tomato';
$this->giveMeSoup($soup->box());
$this->giveMeBean($soup->box()->unbox());
$this->giveMeBean($soup);
}
示例10: testTwoWordsUnderscoredBean
/**
* Cant have underscored beans
* @expectedException \RedBeanPHP\RedException
*/
public function testTwoWordsUnderscoredBean()
{
R::freeze(false);
$testName = R::dispense('test_name');
$id = R::store($testName);
R::load('test_name', $id);
R::freeze($this->frozen);
}
示例11: testToStringIssue512
/**
* Test fix for issue #512 - thanks for reporting Bernhard H.
* OODBBean::__toString() implementation only works with C_ERR_IGNORE
*
* @return void
*/
public function testToStringIssue512()
{
R::setErrorHandlingFUSE(\RedBeanPHP\OODBBean::C_ERR_FATAL);
$boxedBean = R::dispense('boxedbean');
$str = (string) $boxedBean;
asrt($str, '{"id":0}');
//no fatal error
R::setErrorHandlingFUSE(FALSE);
}
示例12: TypeColumn
/**
* Test meta column type.
*
* @return void
*/
public function TypeColumn()
{
$book = R::dispense('book');
$page = R::dispense('page');
$page->book = $book;
R::store($page);
pass();
asrt($page->getMeta('cast.book_id'), 'id');
}
示例13: testNamedParamsInSnippets
/**
* Test usage of named parameters in SQL snippets.
* Issue #299 on Github.
*
* @return void
*/
public function testNamedParamsInSnippets()
{
testpack('Test whether we can use named parameters in SQL snippets.');
R::nuke();
$book = R::dispense('book');
$page = R::dispense('page');
$book->title = 'book';
$book->sharedPage[] = $page;
R::store($book);
//should not give error like: Uncaught [HY093] - SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
$books = $page->withCondition(' title = :title ', array(':title' => 'book'))->sharedBook;
asrt(count($books), 1);
//should not give error...
$books = $page->withCondition(' title = :title ', array(':title' => 'book'))->sharedBook;
asrt(count($books), 1);
R::nuke();
$book = R::dispense('book');
$page = R::dispense('page');
$book->title = 'book';
$book->comment = 'comment';
$page->title = 'page';
$book->ownPage[] = $page;
R::store($book);
//should also not give error..
$count = $book->countOwn('page');
asrt($count, 1);
$book = $book->fresh();
//should also not give error..
$count = $book->withCondition(' title = ? ', array('page'))->countOwn('page');
asrt($count, 1);
$book = $book->fresh();
//should also not give error..
$count = $book->withCondition(' title = :title ', array(':title' => 'page'))->countOwn('page');
asrt($count, 1);
$book = $book->fresh();
$pages = $book->withCondition(' title = :title ', array(':title' => 'page'))->ownPage;
asrt(count($pages), 1);
//test with duplicate slots...
$page = reset($pages);
$page2 = R::dispense('page');
$page2->ownPage[] = $page;
R::store($page2);
$page2 = $page2->fresh();
$pages = $page2->withCondition(' title = :title ', array(':title' => 'page'))->ownPage;
asrt(count($pages), 1);
//test with find()
$books = R::getRedBean()->find('book', array('title' => array('book')), ' AND title = :title ', array(':title' => 'book'));
asrt(count($books), 1);
$books = R::getRedBean()->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' AND title = :title ', array(':title' => 'book'));
asrt(count($books), 1);
//just check numeric works as well...
$books = R::getRedBean()->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' AND title = ? ', array('book'));
asrt(count($books), 1);
//just extra check to verify glue works
$books = R::getRedBean()->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' ORDER BY id ');
asrt(count($books), 1);
}
示例14: testUTF8
/**
* Test UTF8 handling.
*
* @return void
*/
public function testUTF8()
{
$str = '𠜎ὃ𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜';
$bean = R::dispense('bean');
$bean->bla = $str;
R::store($bean);
$bean = R::load('bean', $bean->id);
asrt($bean->bla, $str);
pass();
}
示例15: testNoArrayMetaAccess
/**
* You cant access meta data using the array accessors.
*
* @return void
*/
public function testNoArrayMetaAccess()
{
$bean = R::dispense('bean');
$bean->setMeta('greet', 'hello');
asrt(isset($bean['greet']), FALSE);
asrt(isset($bean['__info']['greet']), FALSE);
asrt(isset($bean['__info']), FALSE);
asrt(isset($bean['meta']), FALSE);
asrt(count($bean), 1);
}