CakePHP 连接两个表

CakePHP joing two tables

我在查看我的数据时遇到问题,我需要 return 一年,ex(2010,2011,2012,2013) 而不是 return 年份 ID,我使用了包含但它没有用,显示数据的正确方法是什么?! 这是输出 array: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')


));

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

另外,请注意,默认情况下(按照 cake 惯例)模型名称是单数的,因此,包含它应该是 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' 
        ) 
    )
    );