Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> RFC użytkownicy i grupy
zajonc
post 7.09.2011, 10:29:10
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.01.2010

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


witam.

chciałbym się dowiedzieć, co sądzicie o takim zaprojektowaniu klas do obsługi użytkowników i grup.
będą używać 3 tabel z bazy danych:
- users: id, name, hash, email // ([nazwa]:[pola])
- membership: id, uid, gid
- groups: id, name

będę wdzięczny za jakiekolwiek sugestie

  1. class user
  2. {
  3. public function __construct ( $uid ) { ... }
  4. public function get_groups () { ... }
  5. public function get_name () { ... }
  6. public function set_name ( $name ) { ... }
  7. public function get_hash () { ... }
  8. public function set_hash ( $hash ) { ... }
  9. public function get_email () { ... }
  10. public function set_email ( $email ) { ... }
  11. }
  12.  
  13.  
  14. #####
  15.  
  16.  
  17. class group
  18. {
  19. public function __construct ( $gid ) { ... }
  20. public function get_users () { ... }
  21. public function count_users () { ... }
  22. public function user_add ( $uid ) { ... }
  23. public function user_rm ( $uid ) { ... }
  24. public function user_exists ( $uid ) { ... }
  25. public function get_name () { ... }
  26. public function set_name ( $name ) { ... }
  27. }
  28.  
  29.  
  30. #####
  31.  
  32.  
  33. class groups
  34. {
  35. public function add ( $name ) { ... }
  36. public function rm ( $gid ) { ... }
  37. public function exists ( $value, $key = 'id' ) { ... } // $key = ( id, name )
  38. public function get_all () { ... }
  39. public function count () { ... }
  40. }
  41.  
  42.  
  43. #####
  44.  
  45.  
  46. class users
  47. {
  48. public function add ( $name ) { ... }
  49. public function rm ( $gid ) { ... }
  50. public function exists ( $value, $key = 'id' ) { ... } // $key = ( id, name )
  51. public function get_all () { ... }
  52. public function count () { ... }
  53. public function check ( $login, $pass ) { ... }
  54. final public function hash ( $hash ) { ... }
  55. }


Ten post edytował zajonc 7.09.2011, 10:40:31
Go to the top of the page
+Quote Post
bastard13
post 7.09.2011, 10:56:22
Post #2





Grupa: Zarejestrowani
Postów: 664
Pomógł: 169
Dołączył: 8.01.2010
Skąd: Kraków

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


1) W klasach User i Group masz settery i gettery do każdej wartości, więc jaki jest ich sens? Przypuszczam, że i tak będziesz wysyłał zawsze pełny zestaw danych więc zastanów się nad zastąpieniem tych metod: setData(array $data).
2) W konstruktorze nie powinno być parametru id, bo skąd wiesz jaki id będzie miał rekord? Przypuszczam, że kolumna jest auto inkrementowana. Id powinien być ustawiany dopiero po zapisie User/Group w bazie.
3) Groups i Users powinny rozszerzać klase abstrakcyjną np. TableAbstract, ponieważ w innym wypadku będziesz miał sporo kodu, który będzie duplikował się.
4) Metody add() i remove() powinny przyjmować jako parametr obiekt klasy User/Group, ponieważ to właśnie je chcesz zapisać/usunąć w/z bazy.
5) Czy rzeczywiście jest ci potrzebna metoda exist() ? Zastąpiłbym ją raczej find(), która zwróci albo obiekt (czyli rekord jest) albo false (brak rekordu).
6) Metody check() i hash() z klasy Users powinny wylecieć. check() jest metodą autoryzacji, więc powinna się znaleźć w tego typu klasie, natomiast hash() przypuszczam, że wykonuje jakieś operacje na stringu, więc na to konto również powinna być odpowiednia klasa.


--------------------
Go to the top of the page
+Quote Post
zajonc
post 7.09.2011, 20:47:31
Post #3





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.01.2010

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


ad. 2. chciałem korzystać np. z user mniej więcej tak:
  1. $user = new user (3);
  2. echo $user->get_name();

istnieje usera sprawdziałbym users::exists()
taki miałem plan.

mógłbyś coś wiedzieć o TableAbstract ? prototypy mile widziane wink.gif
Go to the top of the page
+Quote Post
bastard13
post 7.09.2011, 22:18:36
Post #4





Grupa: Zarejestrowani
Postów: 664
Pomógł: 169
Dołączył: 8.01.2010
Skąd: Kraków

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


Co do kwestii klasy user, to tworzenie jej obiektu powinno wyglądać tak:
  1. $user = new User();

Bez id, ponieważ jeżeli podajesz id w konstruktorze oznacza to, że użytkownik nie jest tworzony tylko pobierany z bazy.

Pobieranie użytkownika z bazy powinno wyglądać tak:
  1. $user = Users::find(3);

I w tym momencie, jeżeli istnieje użytkownik o id 3, to klasa Users go zwraca, a jeżeli nie istenieje, to zwraca false. Dlatego też nie jest ci potrzebna metoda exists()

Coś o TableAbstract:
  1. abstract class TableAbstract
  2. {
  3. public function find($id) {}
  4. public function delete($id) {}
  5. public function update($id, array $data) {}
  6. abstract protected function _getTableName();
  7. }
  8.  
  9. class Users extends TableAbstract
  10. {
  11. protected function _getTableName() {
  12. return 'Users';
  13. }
  14. }
  15.  
  16.  
  17. class Groups extends TableAbstract
  18. {
  19. protected function _getTableName() {
  20. return 'Groups';
  21. }
  22. }


Mniej więcej o to chodzi. Jak dobrze przemyślisz sprawę to metody update, delete i find różnią się tak naprawdę jedynie nazwą tabeli.


--------------------
Go to the top of the page
+Quote Post
zajonc
post 8.09.2011, 16:35:29
Post #5





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.01.2010

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


w klasie user nie operuje na danych w bazie, tylko seterami/geterami pracuje na zmiennych, tak?
dopiero users::update () dłubie w bazie.

  1. class user
  2. {
  3. private $id;
  4. private $name;
  5. private $pass;
  6.  
  7. function __construct ( $uid, $name, $pass )
  8. {
  9. $this->id = $uid;
  10. # name, pass
  11. }
  12.  
  13. function get_name () { /* .. */ }
  14. function get_id () { /* .. */ }
  15. function get_pass () { /* .. */ }
  16. function set_pass ( $pass ) { /* .. */ }
  17. }

to już chyba lepiej wygląda. jeszcze chciałbym zaimplementować grupy i pola dodatkowe (np. imię, nazwisko). to też w klasie user?
jeśli tak, go kiedy powinienem pobierać dane z bazy?

Ten post edytował zajonc 8.09.2011, 16:36:17
Go to the top of the page
+Quote Post
bastard13
post 8.09.2011, 16:55:14
Post #6





Grupa: Zarejestrowani
Postów: 664
Pomógł: 169
Dołączył: 8.01.2010
Skąd: Kraków

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


Cytat
jeśli tak, go kiedy powinienem pobierać dane z bazy?

Cytat
Pobieranie użytkownika z bazy powinno wyglądać tak:
  1. $user = Users::find(3);


A co do metody update w TableAbstract, to trochę się rozpędziłem:)
To klasa User i Group powinna posiadać metodę save(), która przy nowych obiektach dodaje je do bazy, a przy tych wyciągniętych z bazy - zapisuje zmiany.

Ten post edytował bastard13 8.09.2011, 16:55:46


--------------------
Go to the top of the page
+Quote Post
zajonc
post 8.09.2011, 17:38:20
Post #7





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.01.2010

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


users::find($uid) zwraca użytkownika ze wszystkimi wypełnionymi polami?
tj. id, name, pass, groups, custom_fields.

czy też już user::get_groups dba o aktualne dane?

  1. <?php
  2.  
  3.  
  4.  
  5.  
  6.  
  7. class user
  8. {
  9.  
  10. private $id;
  11. private $name;
  12. private $pass;
  13. private $customs = array ();
  14. private $groups = array ();
  15.  
  16.  
  17.  
  18. function __construct ( $uid, $name, $pass, array $groups, array $customs )
  19. {
  20. $this->id = (int) $uid;
  21. # name, pass, customs, groups
  22. }
  23.  
  24.  
  25.  
  26. function get_id ()
  27. {
  28. return $this->id;
  29. }
  30.  
  31.  
  32.  
  33. function get_name ()
  34. {
  35. return $this->name;
  36. }
  37.  
  38.  
  39.  
  40. function get_pass ()
  41. {
  42. return $this->pass;
  43. }
  44.  
  45.  
  46.  
  47. function set_pass ( $pass )
  48. {
  49. $this->pass = $pass;
  50. }
  51.  
  52.  
  53.  
  54. # grupy
  55.  
  56. function get_groups ()
  57. {
  58. return $this->groups;
  59. }
  60.  
  61.  
  62.  
  63. function add_group ( $gid )
  64. {
  65. $this->groups[] = $gid;
  66. }
  67.  
  68.  
  69.  
  70. function rm_group ( $gid )
  71. {
  72. unset ( $this->groups[ array_search ( $gid, $this->groups ) ] );
  73. }
  74.  
  75.  
  76.  
  77. function is_in_group ( $gid )
  78. {
  79. return in_array ( $gid, $this->groups );
  80. }
  81.  
  82.  
  83.  
  84. # pola dodatkowe
  85.  
  86. function get_customs ()
  87. {
  88. return $this->customs;
  89. }
  90.  
  91.  
  92.  
  93. function add_custom ( $id, $value )
  94. {
  95. $this->customs[ $id ][] = $value;
  96. }
  97.  
  98.  
  99.  
  100. function rm_custom ( $id, $value )
  101. {
  102. unset ( $this->customs[ $id ][ array_search ( $value, $this->customs[ $id ] ) ] );
  103. }
  104.  
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. class users
  116. {
  117.  
  118.  
  119.  
  120.  
  121.  
  122. function __construct ()
  123. {
  124. $this->db = kernel::get ( 'db' );
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. function update ( user $user )
  132. {
  133. # nanoszenie zmian do tabel: users, memberships, users_customs
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. function save ( user $user )
  141. {
  142. $uid = $this
  143. -> db
  144. -> prepare
  145. (
  146. "INSERT INTO `users`
  147. (
  148. `id`,
  149. `name`,
  150. `pass`
  151. )
  152. VALUES
  153. (
  154. NULL ,
  155. ':name',
  156. ':hash'
  157. )" )
  158. -> bind ( 'name', $user->name )
  159. -> bind ( 'hash', auth::hash ( $user->pass ) )
  160. -> exec ()
  161. -> insert_id ();
  162.  
  163. # zapis customs i groups do bazy
  164. }
  165.  
  166.  
  167.  
  168. function find ( $uid )
  169. {
  170. $r = # pobieranie z bazy ( $name, $pass )
  171.  
  172. if ( empty ( $r[0] ) )
  173. {
  174. return false;
  175. }
  176.  
  177. #pobieranie $customs i $groups z bazy
  178.  
  179. extract ( $r[0] );
  180. return new user ( $uid, $name, $pass, $groups, $customs );
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
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 Wersja Lo-Fi Aktualny czas: 29.05.2024 - 07:45