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
 
Start new topic
Odpowiedzi (1 - 2)
mar22
post
Post #2





Grupa: Zarejestrowani
Postów: 25
Pomógł: 1
Dołączył: 12.09.2009

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


najlepiej wygeneruj kod przez gii:)
Go to the top of the page
+Quote Post
sowiq
post
Post #3





Grupa: Zarejestrowani
Postów: 1 890
Pomógł: 339
Dołączył: 14.12.2006
Skąd: Warszawa

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


W pliku _view.php:
  1. $product = $data->product;
  2.  
  3. echo $product->name;


Ten post edytował sowiq 12.03.2013, 16:02:50
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 21.08.2025 - 03:43