doujiang5211 2011-01-19 18:17
浏览 21
已采纳

CakePHP - 关联模型的条件

Problem in short: I want to set conditions on an associated model. How do I do it?

I'm having a problem with the associations in my CakePHP application.

The associations looks like this:

Event has many EventSum belongs to Account has many AccountUser belongs to User
Event has many EventDebt ... (the rest is the same as above)
Event also belongs to User

The application is a private econonmy program in PHP and uses the CakePHP framework.

An Event is a financial event, a purchase, transaction between accounts etc. It only holds information about date, title and user.

An EventSum holds information about an Account and how much to debit or credit (in one column, just positive or negative).

Account holds information about title of the account.

AccountUser holds an id of an Account and a User. This indicates that


So, now I want to fetch Events based on what accounts a User is associated to. How can I do this?

I want to fetch the following info: Event, together with the EventSum. The Events are fetched from Accounts where the User has access.

Thanks for any help, /Magnus

  • 写回答

1条回答 默认 最新

  • douxingti9307 2011-02-10 02:52
    关注

    It seems like you want to be able to query your Event class with the following conditions:

    'Account.user_id =' => $userId
    

    Is my assumption correct?

    When doing queries which require conditions on associated models, you can use either the Containable behaviour (comes with CakePHP 1.3) or the 'Linkable' behaviour (which can be found here).

    What happens when you try this (be sure to attach the Containable behaviour to your models first):

    $condition = array('Account.user_id =' => $userId);
    $contain = array('EventSum' => array('Account'), 'EventDebt' => array('Account'));
    $result = $this->Event->find('all', compact('condition', 'contain'));
    

    Note that you might experience issues when 'containing' both EventSum and EventDebt if both of their associations to Account use the same alias name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?