蛋糕模型协会

时间:2014-02-17 17:08:09

标签: cakephp

我不太确定如何使用CakePHP完成此任务。我有3个型号:

ReportType: id/name

Example Data:

1/Report1
2/Report2

ReportData: id/report_type_id (matches ReportType)/row_number (matches row number in CSV)/data

Example Data:

1/1/1/longstring1
2/1/1/longstring1-2
3/1/1/longstring1-3
4/1/2/longstring2
5/1/3/longstring3
6/1/4/longstring4

PaymentData: id/report_type_id (matches ReportType)/row_number (matches row number in CSV)/payment_amount

Example Data:

1/1/1/5
2/1/2/6
3/1/3/78
4/1/5/9

ReportType:
HasMany: ReportData, PaymentData

示例查询:

$this->ReportType->contain('PaymentData');
$payment_data = $this->ReportType->find('first');

现在,这会产生:

[ReportType]
     id => 1
     name => Report1

[PaymentData]
    [0] =>
        id => 1
        report_type_id => 1
        row_number => 1
        payment_amount => 5
    [1] =>
        id => 1
        report_type_id => 1
        row_number => 2
        payment_amount => 6

    (and so on)

我想要做的是每个PaymentData行,能够匹配report_type_id和row_number在ReportData中匹配的行(我只需要1行)。例如,我希望返回的数据为:

[ReportType]
    id => 1
    name => Report1

[PaymentData]
    [0] =>
        id => 1
        report_type_id => 1
        row_number => 1
        payment_amount => 5

        [ReportData] => (matched by report_type_id and row_number for PaymentData above)
            [0] =>
                id => 1
                report_type_id => 1
                row_number => 1
                data => longstring1
    [1] =>
        id => 1
        report_type_id => 1
        row_number => 2
        payment_amount => 6

        [ReportData] => (matched by report_type_id and row_number for PaymentData above)
            [0] =>
                id => 4
                report_type_id => 1
                row_number => 2
                data => longstring2

1 个答案:

答案 0 :(得分:0)

您可以尝试在contain

中进行匹配
$payment_data = $this->ReportType->find('first', array(
    'contain' => array(
        'PaymentData' => array(
            'ReportData' => array(
                'conditions' => array(
                    'PaymentData.report_type_id' => 'ReportData.report_type_id',
                    'PaymentData.row_numbrer' => 'ReportData.row_number'
                )
            )
        )
    )
));

假设ReportType有很多PaymentData,而且这个ReportData有很多{{1}}