redbean - nested beans - what's going wrong?

2019-02-25 13:41发布

问题:

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);


标签: php redbean