【问题标题】:query over many to many relationship in redbeanphp在redbeanphp中查询多对多关系
【发布时间】:2015-06-14 12:25:50
【问题描述】:

我有两个表,分别是“user”和“estate”,我用这段代码在这些表之间建立了多对多的关系:

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

“estatetrans”是包含这两个表之间关系的表的名称:

现在我想通过过滤 trtype 列来查询“estatetrans”表。

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

$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 方法完成这项工作?

【问题讨论】:

    标签: php mysql redbean


    【解决方案1】:

    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 列过滤的交易。

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

    干杯 嘉柏

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-07
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 2021-10-03
      相关资源
      最近更新 更多