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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真