2015-01-17 5 views
1

У меня есть проблема с просмотром моих данных, мне нужно вернуть год, ex (2010,2011,2012,2013) вместо того, чтобы возвращать идентификатор года, я использовал, но он не работал, что это правильный способ отображения данных?! Вот вывод массива: массив (CakePHP, соединяющий две таблицы

(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') 


)); 
+0

Установить рекурсивный на 1 (или выше), чтобы загрузить ваши отношения ... Это должно дать вам данные из связанных таблиц. – Bob

+0

@Bob рекурсивный - это не очень хорошая идея, он выберет все ненужные модели/поля. – dav

ответ

2

Вам просто нужно указать точные поля для каждой модели (в том числе связанных с ними моделей), в противном случае только идентификатор возвращается (и это хорошо, потому что, если бы все поля были, если у вас есть текстовый столбец в db с КБ данных - вы не хотите его извлекать)

также обратите внимание, что по умолчанию (и по соглашениям о торте) имена моделей являются единственными, поэтому, insi de содержать это должно быть 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' 
     ) 
    ) 
    ); 
+0

Спасибо вам большое :) –

Смежные вопросы