Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [CakePHP] relacje
kicaj
post
Post #1





Grupa: Zarejestrowani
Postów: 1 640
Pomógł: 28
Dołączył: 13.02.2003
Skąd: Międzyrzecz/Poznań

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


Probuje stworzyc relacje w ten sposob:
Kod
Article hasOne User
User hasMany Article
User hasMany Comments
Articles hasMany Comments


Tabela 'comments' chcialbym zeby wygladala mniej wiecej tak:
Kod
id
content
user_id
model
foreign_key
...


Przykladowe wiersze w tabeli 'comments':
Kod
1, 'Witaj kolego!' 123, User, 3        // user_id=123 dodal komentarz do uzytkownika o id=3
2, 'Dobry artykul!', 234, Article, 42    // user_id=234 dodal komentarz do artykulu o id=42


Czy to jest dobry sposob dla tabeli 'comments', dobre rozumowanie.
I w jaki sposob mialby dzialac model Comment?

Ten post edytował kicaj 10.01.2009, 02:30:52
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Rafal Filipek
post
Post #2





Grupa: Zarejestrowani
Postów: 274
Pomógł: 54
Dołączył: 2.05.2006
Skąd: Nadarzyn

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


no to ok, wiec zrobil bym to tak
Kod
tabela comments
---------------
id
content
model
model_id


i teraz tak
  1. <?php
  2. class Article extends AppModel{
  3.    public $hasMany = array(
  4.        'Comment' => array(
  5.            'foreignKey' => 'model_id',
  6.            'conditions' => array('Comment.model' => 'Article')
  7.        )
  8.    );
  9. }
  10. ?>


i tak samo dla userow

a model Comment
  1. <?php
  2. class Comment extends AppModel{
  3.    public $belongsTo = array(
  4.        'Article' => array(
  5.            'foreignKey' => 'model_id',
  6.            'conditions' => array('Comment.model' => 'Article')
  7.        ),
  8.        'User' => array(
  9.            'foreignKey' => 'model_id',
  10.            'conditions' => array('Comment.model' => 'User')
  11.        )
  12.    );
  13. }
  14. ?>
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: 13.10.2025 - 23:31