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
$dataProvider->pagination->pageSize=100;
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'enablePagination'=>false,
)); ?>