Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [inny] Yii a relacje między tabelami
Barton
post
Post #1





Grupa: Zarejestrowani
Postów: 70
Pomógł: 2
Dołączył: 26.10.2008

Ostrzeżenie: (0%)
-----


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:
  1. public function relations()
  2. {
  3. return array(
  4. 'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
  5. 'files' => array(self::HAS_MANY, 'File', 'product_id'),
  6. );
  7. }


Dla klasy file:
  1. public function relations()
  2. {
  3. // NOTE: you may need to adjust the relation name and the related
  4. // class name for the relations automatically generated below.
  5. return array(
  6. 'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
  7. );
  8. }


Kontroler File zawiera metodę actionIndex:
  1.  
  2. public function actionIndex()
  3. {
  4. $this->pageTitle = Yii::app()->name.' - '.'Download';
  5. $dataProvider=new CActiveDataProvider('File', array(
  6. 'criteria'=>array(
  7. 'condition'=>'product_id IS NOT NULL'
  8. )
  9. ));
  10. $this->render('index',array(
  11. 'dataProvider'=>$dataProvider,
  12. ));
  13. }


W jaki sposób w pliku widoku mogę odnieść się do danych z relacyjnej tabeli product? Plik widoku:
  1. <?php
  2. $dataProvider->pagination->pageSize=100;
  3.  
  4. $this->widget('zii.widgets.CListView', array(
  5. 'dataProvider'=>$dataProvider,
  6. 'itemView'=>'_view',
  7. 'enablePagination'=>false,
  8. )); ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.12.2025 - 15:38