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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应