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,
)); ?>
Page 1 of 1
Problem Z Odniesieniem Się Do Danych Z Relacyjnie Powiązanej Tabeli
#2
Posted 11 March 2013 - 07:12 AM
W miejscu gdzie generujesz DataProvider w parametrach musisz użyć klauzuli "with", np:
a w widoku odwołujesz się:
więcej, szukaj w necie "DataProvider with relations"
$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"
Share this topic:
Page 1 of 1

Help











