dpziir0079 2014-03-20 09:39
浏览 23
已采纳

RedBean关系定义

I'm not following, how exactly I define relations in RedBean, not on the fly.

I have a user which can have a parent user that is considered an employer. On the opposite side, a user can have employees which are also users.

How would I go about defining this?

I'd like the interface to be something of this sort:

$user = R::load('user', $id);
var_dump($user->hasEmployees); // show all employees
var_dump($user->ownEmployer); // show my employer

But it looks like, I have to define them every time like this:

$user->hasEmployees = R::findAll('user', 'employer_id = ?', array($user->id));
$user->ownEmployer  = R::load('user', $user->employer_id);

Which kind of defeats the purpose of having a dynamic system. Since there's no real model (or not a very extensive one), I'm assuming you create the relations one time and use them, and RedBean knows to do so next time on?

Or do I have to define that relationship whenever I'm accessing the model? (could I use the dispense() method in the model in that case?)

If so, how does that work exactly?

What am I missing?

Thanks!

  • 写回答

1条回答 默认 最新

  • douhuxi4145 2014-03-26 22:12
    关注

    You should talk a look at the relations section on redbeanphp.com. Relations are created by strict naming conventions. In your case it would work like this:

    // Dispense the beans
    list($employer, $employee1, $employee2) = R::dispense('employee', 3);
    
    // Create relations
    $employer->ownEmployeeList = array($employee1, $employee2);
    
    // Demonstrate relations
    var_dump($employer->ownEmployeeList); // returns $employee1 and $employee2
    var_dump($employee1->employee); // returns $employer
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?