Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [inny] Yii a relacje między tabelami
Forum PHP.pl > Forum > PHP > Frameworki
Barton
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. )); ?>
mar22
najlepiej wygeneruj kod przez gii:)
sowiq
W pliku _view.php:
  1. $product = $data->product;
  2.  
  3. echo $product->name;
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.