Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Kohana] 3 - zwracanie w metodach ORM, return nie dziala
lukaskolista
post
Post #1





Grupa: Zarejestrowani
Postów: 872
Pomógł: 94
Dołączył: 31.03.2010

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


Witam. Mam model ORM
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class Model_File extends ORM {
  4.  
  5. protected
  6. $belongs_to = array('folders');
  7.  
  8. private
  9. $file_source = array();
  10.  
  11. public function __construct() {
  12. parent::__construct();
  13. }
  14.  
  15.  
  16. public function get_file_source($folder_id = null) {
  17. if ($folder_id === null) {
  18. $folder_id = $this->folder_id;
  19. }
  20.  
  21. $folder = ORM::factory('folder', $folder_id);
  22.  
  23. $this->file_source[] = $folder->name;
  24.  
  25. if ($folder->parent_id != 0) {
  26. $this->get_file_source($folder->parent_id);
  27. } else {
  28. $this->file_source = array_reverse($this->file_source);
  29. $file_source = '';
  30. foreach ($this->file_source as $folder_name) {
  31. $file_source .= $folder_name.'/';
  32. }
  33.  
  34. $file_source = substr($file_source, 0, -1);
  35. return $file_source;
  36. }
  37. }
  38. }
  39.  
  40. ?>


metoda get_file_source zwraca sciezke do pliku stworzona na podstawie bazy danych. Tworzy ja poprawnie, jednak nie zwraca jej do kontrolera. Domyslam sie, ze problem jest w return i w wykorzystaniu kohana 3 (w kohana 2) zwykle retur dzialalo. Wie ktos, co jest nie tak? Z gory dziekuje za pomoc
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
phpion
post
Post #2





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Jeżeli to było skierowane do mnie to przykład. Mamy 2 modele:
  1. class Comment_Model extends ORM {
  2. protected $belongs_to = array(
  3. 'user'
  4. );
  5. }
  6.  
  7. class User_Model extends ORM {
  8. protected $has_many = array(
  9. 'comments'
  10. );
  11. }

W kodzie wyświetlamy wszystkie komentarze wraz z nazwą użytkownika:
  1. $comments = ORM::factory('comment')
  2. ->find_all()
  3. ;
  4.  
  5. foreach ($comments as $comment) {
  6. echo $comment->content.' ('.$comment->user->username.')';
  7. }

I już. Jednak dla każdego komentarza pobierany z bazy danych będzie użytkownik (czyli mamy N+1 zapytań SQL). Aby to poprawić modyfikujemy pobieranie komentarzy:
  1. $comments = ORM::factory('comment')
  2. ->with('user') // yeah!
  3. ->find_all()
  4. ;

Gdybyśmy mieli dalsze zagłębienia relacji (np. użytkownik należy do kraju) to (poza definicją relacji w modelu) moglibyśmy zrobić:
  1. $comments = ORM::factory('comment')
  2. ->with('user')
  3. ->with('user:country') // to dopiero yeah!
  4. ->find_all()
  5. ;

No i potem w kodzie:
  1. $comment->user->country->name;


PS: Powyższe kody dotyczą ORM dla KO2.

Ten post edytował phpion 25.07.2010, 17:30:49
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: 12.10.2025 - 00:11