Problem Z Odniesieniem Się Do Danych Z Relacyjnie Powiązanej Tabeli

Witam,

Mam problem z pewnym zapytaniem. Istnieją dwie tabele: product i file. Jeden produkt ma przypisane kilka plików, w tabeli file kluczem obym jest product_id. W klasach modelu dziedziczącej po CActiveRecord dodalem takie metody:

Dla klasy product:

public function relations()


{


	return array(


		'category' => array(self::BELONGS_TO, 'Category', 'category_id'),


		'files' => array(self::HAS_MANY, 'File', 'product_id'),


	);


}

Dla klasy file:

public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'product' => array(self::BELONGS_TO, 'Product', 'product_id'),


	);


}

Kontroler File zawiera metodę actionIndex:

public function actionIndex()

{


            $this->pageTitle = Yii::app()->name.' - '.'Download';


	$dataProvider=new CActiveDataProvider('File', array(


                'criteria'=>array(


                    'condition'=>'product_id IS NOT NULL'


                )


            ));


	$this->render('index',array(


		'dataProvider'=>$dataProvider,


	));


}

W jaki sposób w pliku widoku mogę odnieść się do danych z relacyjnej tabeli product? Plik widoku:

<?php

    &#036;dataProvider-&gt;pagination-&gt;pageSize=100;


    


    &#036;this-&gt;widget('zii.widgets.CListView', array(


'dataProvider'=&gt;&#036;dataProvider,


'itemView'=&gt;'_view',


    'enablePagination'=&gt;false,

)); ?>

W miejscu gdzie generujesz DataProvider w parametrach musisz użyć klauzuli "with", np:




$dataProvider=new CActiveDataProvider('Students',array(

  'criteria'=>array(

    'with'=>array('teachers'),

    'condition'=>"teachers.teacher_id='$userId'"

    'together'=>true,

  )

));

a w widoku odwołujesz się:




//gdzie data to Student

$data->teachers

//gdzie teachers jest tablicą aktywnych rekordów lub tylko jedym aktywnem rekordem w zależności od tego jaka jaest relacja w modelu



więcej, szukaj w necie "DataProvider with relations"