redbean - nested beans - what's going wrong?

天涯浪子 提交于 2019-12-01 23:35:48

问题


am trying to get a simple nested bean relationship - what am i missing?

i really like redbean's simple ORM syntax and really want to use it, but i can't seem to get it to work for me!

Is there anything else similar to this that's a bit more mature maybe? I want something light and simple to build wordpress plugins with but need to know i can rely on it in the future...

am starting to think about just using ezsql/sqlite but would rather not :/

Thanks for any help...

function p($s){
    $s = htmlentities(print_r($s,true));
    echo "<pre>$s</pre>";
}

require('rb.php');

R::setup('sqlite:dbfile.sql'); //sqlite\

R::debug(true);

// R::wipe('book');
// R::wipe('author');

$book = R::dispense( 'book' );
$book->title = 'Boost development with RedBeanPHP';

$a = R::dispense('author');
$a->name = "Dave";

$book->author = $a;

list($page1,$page2) = R::dispense('page',2);

$book->pages = array($page1,$page2);


$id = R::store($book);

echo $b = R::load('book',$id);
echo $b->author->name;

I'm getting the following error when trying to store the pages....

Fatal error: Uncaught exception 'RedBean_Exception_Security' with message 'Invalid Bean: property pages ' in /Users/sig/Sites/redbean/rb.php:1508 Stack trace: #0 /Users/sig/Sites/redbean/rb.php(1587): RedBean_OODB->check(Object(RedBean_OODBBean)) #1 /Users/sig/Sites/redbean/rb.php(2523): RedBean_OODB->store(Object(RedBean_OODBBean)) #2 /Users/sig/Sites/redbean/index.php(30): RedBean_Facade::store(Object(RedBean_OODBBean)) #3 {main} thrown in /Users/sig/Sites/redbean/rb.php on line 1508


回答1:


the problem was that the array needs to have the same name as the objects in it, but with either own or shared prefixed depending on the relationship...

$book->ownPage = array($page1,$page2);


来源:https://stackoverflow.com/questions/8112383/redbean-nested-beans-whats-going-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!