同一个Bean Redbean的多个共享列表

时间:2017-11-10 13:58:56

标签: php javabeans shared redbean

如果我有'产品'bean和'上传'bean,我有两个属性:

$product->sharedImageList;
$product->sharedManualList;
$product->sharedVideoList; // just here for another property 
                           //relating to another bean elsewhere

他们都使用'上传'bean。但我不能为他们添加别名。我可以不为两个共享列表使用相同的bean吗?我知道共享bean列表需要与bean名称相同,但是fetchAs在下面工作...

$teacher = $c->fetchAs( 'person' )->teacher;

我试过了

$product->fetchAs('upload')->sharedImageList
//getting desperate now
$product->via('upload')->sharedImageList
$product->alias('upload')->sharedImageList

您可以使用它来测试我的问题

<?php
include_once 'rb.php';

error_reporting( E_ALL );
R::addDatabase( "my_site", "mysql:host=127.0.0.1;dbname=my_db", "root", "", false );
R::selectDatabase( 'my_site' );

\R::nuke();

$images  = [
    ["id" => 1,'name'=>"image"],
    ["id" => 2,'name'=>"test image2"],
];
$manuals = [
    ["id" => 1,'name'=>"test1"],
    ["id" => 2,'name'=>"test manual2"],
];

$product = R::findOneOrDispense( 'product', ' id=? ', [1] );

$product->sharedImageList  = []; //empty / create new shared image list
$product->sharedManualList = []; //empty / create new shared manual list
$product->name             = "im a product"; // give product a name for giggles

foreach ($images as $object) {
    $upload                     = R::findOneOrDispense( 'upload', " id = ? ", [$object['id']] );
    $upload->name               = $object['name'];
    $product->sharedImageList[] = $upload;
}

foreach ($manuals as $object) {
    $upload                      = R::findOneOrDispense( 'upload', " id = ? ", [$object['id']] );
    $upload->name                = $object['name'];
    $product->sharedManualList[] = $upload;
}

$product = R::load( 'product', R::store( $product ) );

echo '<pre>';
echo "\n\n<b>// this should return 2 beans of images that were uploaded</b>\n";
echo json_encode( $product->sharedImageList, JSON_PRETTY_PRINT );


echo "\n\n<b>// this should return 2 beans of manuals that were uploaded</b>\n";
echo json_encode(  $product->sharedManualList, JSON_PRETTY_PRINT );


echo "\n\n<b>// this is the only way i can access the shared lists, which is the name of the (upload) bean</b>\n";
echo json_encode( $product->sharedUploadList, JSON_PRETTY_PRINT );
echo "\n\n\n";
echo json_encode($product,JSON_PRETTY_PRINT);

echo '</pre>';

1 个答案:

答案 0 :(得分:0)

不支持几个bean之间的many to many别名关系! (让我们说bean1bean2

解决方法是使用ad-hoc bean(让我们说bean_m2m表示&#34; m2m-relation&#34;以及一对一对多和多对...像这样的一个关系。

bean1 - (one2manyRelation) - bean_m2m - (many2oneRelation) - bean2

为了简化查询,您可以使用(aggr函数)[https://www.redbeanphp.com/index.php?p=/other_relations#aggr]