查询redbeanphp中的多对多关系

query over many to many relationship in redbeanphp

我有两个 table,它们是 "user" 和 "estate",我用这段代码在 table 之间建立了很多关系:

$user->link("estatetrans", (array("trtype" => $trtype, "indate" => time())))->estate = $estate;

"estatetrans" is name of table that contain relation between these two table :

现在我想通过过滤 trtype 列来查询 "estatetrans" table。

我使用此代码执行此操作:

$trans = R::findAll("estatetrans", "users_id=:uid and trtype=:trt" , array("uid"=>$userId , "trt"=>$trtype)) ; 
    $estates = array() ; 
    foreach ($trans as $tr)
    {
        array_push($estates, $tr->estate) ; 
    }

但我知道这不是一个完美的好校长。 我如何通过 redbeanphp 方法完成这项工作?

RedBeanPHP 的方法是使用 sharedList 而不是 findAll,例如,像这样:

list($e, $t) = R::dispenseAll('estate,transaction');
$e
  ->link('estate_transaction',['type'=>'auction'])
  ->transaction = $t;
R::store($e);
$e = R::findOne('estate');
$x = $e
     ->withCondition(' estate_transaction.type = ? ',['auction'])
     ->sharedTransaction;

$x 现在包含按 estate_transaction.type 列筛选的交易。

请注意,如果您想要更高级的解决方案,也可以重命名 link table。

干杯 嘉宝