cakephp找到几张桌子

时间:2011-12-14 14:40:19

标签: cakephp-2.0

我的问题是我试图从5个不同的表中获取数据,所有表都是相关的,我可以创建一个SQL查询来获得我想要的。在这里。

SELECT accounts.account_number, trailers.id, members.first_name, balances.balance, loans.loan_description, delinquent_loans.days_delinquent, delinquent_loans.amount_delinquent, delinquent_loans.next_contact, delinquent_loans.last_contact
FROM accounts, trailers, members, balances, accounts_trailers, loans, delinquent_loans
WHERE delinquent_loans.loan_id = loans.id
AND loans.accounts_trailer_id = accounts_trailers.id
AND accounts_trailers.account_id = accounts.id
AND accounts_trailers.trailer_id = trailers.id
AND accounts.member_id = members.id
AND balances.accounts_trailer_id = accounts_trailers.id
AND members.first_name
REGEXP '^[a-l]'
ORDER BY delinquent_loans.days_delinquent DESC

我不明白的是如何在cakePHP中以正确的方式进行此查询。我确信我可以使用查询功能并粘贴上一个查询,但我想以正确的方式执行此操作。

public function listAL(){
    $this->recursive = -1;
    $conditions = array('`DelinquentLoan`.`loan_id` = `Loan`.`id`', 
                                '`Loan`.`accounts_trailer_id` = `AccountsTrailer`.`id`', 
                                '`AccountsTrailer`.`account_id` = `Account`.`id`', 
                                '`AccountsTrailer`.`trailer_id` = `Trailer`.`id`', 
                                '`Account`.`member_id` = `Member`.`id`', 
                                '`Balance`.`accounts_trailer_id` = `AccountsTrailer`.`id`', 
                                '`Member`.`first_name` regexp \'^[a-l]\'');
    $fields = array('`Account`.`account_number`, 
                            `Trailer`.`id`, `Member`.`first_name`, 
                            `Balance`.`balance`, 
                            `Loan`.`loan_description`, 
                            `Delinquent_loan`.`days_delinquent`, 
                            `Delinquent_loan`.`amount_delinquent`, 
                            `Delinquent_loan`.`next_contact`, 
                            `Delinquent_loan`.`last_contact`');
    return $this->find('all', array('fields'=> $fields, 
                                                'order'=>array('`DelinquentLoan`.`days_delinquent` desc'),
                                                'conditions'=> $conditions));
}

对此有任何帮助表示赞赏。我看过其他网站和论坛但无济于事。谢谢。

2 个答案:

答案 0 :(得分:0)

有点晚了,但我认为这就是你要找的东西:

Model::saveAssociated(array $data = null, array $options = array())

在食谱Saving Your Data

中找到

答案 1 :(得分:0)

如果模型之间的关联做得好,我不会仅仅通过使用简单的查找('all',...)来看问题,也许你应该更好地指定你想要对你的模型做什么以及它们之间的联系如何。

可包含的行为可能会对您有所帮助。它用于在执行“查找”时剪切和过滤信息:

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html