CakePHP加入两个表

时间:2015-01-17 18:54:39

标签: cakephp join cakephp-2.0

我在查看我的数据时遇到问题,我需要返回一年,ex(2010,2011,2012,2013)而不是返回年份id,我用过包含但它没有用,是什么方法正确显示数据?! 这是输出数组:array(

(int) 0 => array(
        'IndicatorDatum' => array(
            'id' => '188',
            'indicator_id' => '2',
            'report_year_id' => '2',
            'value' => '144063181',
            'comment' => '0',
            'reference' => '0'
        )
    ),
    (int) 1 => array(
        'IndicatorDatum' => array(
            'id' => '163',
            'indicator_id' => '2',
            'report_year_id' => '3',
            'value' => '178104575',
            'comment' => '0',
            'reference' => '0'
        )
    ),
    (int) 2 => array(
        'IndicatorDatum' => array(
            'id' => '137',
            'indicator_id' => '2',
            'report_year_id' => '4',
            'value' => '198637602',
            'comment' => '0',
            'reference' => '0'
        )
    ),
    (int) 3 => array(
        'IndicatorDatum' => array(
            'id' => '5752',
            'indicator_id' => '2',
            'report_year_id' => '5',
            'value' => '1234',
            'comment' => null,
            'reference' => null
        )
    ),
    (int) 4 => array(
        'IndicatorDatum' => array(
            'id' => '5694',
            'indicator_id' => '2',
            'report_year_id' => '6',
            'value' => '100',
            'comment' => '0',
            'reference' => '0'
        )
    ),
    (int) 5 => array(
        'IndicatorDatum' => array(
            'id' => '5271',
            'indicator_id' => '2',
            'report_year_id' => '6',
            'value' => '1',
            'comment' => '0',
            'reference' => '0'
        )
    )
)

模型

    public $belongsTo = array(

        'Indicator' => array(
            'className' => 'Indicator',
            'foreignKey' => 'indicator_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'ReportYear' => array(
            'className' => 'ReportYear',
            'foreignKey' => 'report_year_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Organisation' => array(
            'className' => 'Organisation',
            'foreignKey' => 'organisation_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );

$this->find('all',array(
        'fields' => array('IndicatorDatum.id','IndicatorDatum.indicator_id','IndicatorDatum.report_year_id','IndicatorDatum.value','IndicatorDatum.comment','IndicatorDatum.reference'
        ),'contain' => array(
            'ReportYears'),

        'conditions'=>array('IndicatorDatum.indicator_id' =>'2' , 'IndicatorDatum.organisation_id'=>$organisation_id),
        'order' => array('IndicatorDatum.report_year_id')


));

1 个答案:

答案 0 :(得分:2)

你只需要为每个模型指定确切的字段(包括相关的模型),否则只返回id(这很好,因为如果它是所有字段,如果你有一些文本列怎么办?带有KB数据的数据库 - 您不想检索它)

另外,请注意,默认情况下(以及蛋糕约定)模型名称是单数,因此,内部包含它应该是ReportYear而不是ReportYears。

这是最终查询。

    $this->find('all', array(
        'fields' => array(
            'IndicatorDatum.id',
            'IndicatorDatum.indicator_id',
            'IndicatorDatum.report_year_id',
            'IndicatorDatum.value',
            'IndicatorDatum.comment',
            'IndicatorDatum.reference' 
        ),
        'contain' => array(
            'ReportYear' => array(
                'fields' => array(
                    'ReportYear.id',
                    'ReportYear.year' // not sure what you have as a year field name
                ) 
            ) 
        ),
        'conditions' => array(
            'IndicatorDatum.indicator_id' => '2',
            'IndicatorDatum.organisation_id' => $organisation_id 
        ),
        'order' => array(
            'IndicatorDatum.report_year_id' 
        ) 
    )
    );