Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. class User extends stdClass {
  2. function getName() {
  3. return $this->first_name . " ". $this->last_name;
  4. }
  5. }
  6.  
  7. class Post extends stdClass {
  8.  
  9. /**
  10. * @var User
  11. */
  12. public $author;
  13. }
  14.  
  15. DB::getInstance()->setFetchTableNames(1);
  16. $sql = DB::getInstance()
  17. ->select("p.*, u.*")
  18. ->from("posts p")
  19. ->join("INNER JOIN users u USING(user_id)")
  20. ->where("u.user_id = ?", $user_id)
  21. ->orderBy("p.title");
  22.  
  23. $stmt = $sql->execute();
  24.  
  25. /* @var Post[] $post_collection */
  26. $post_collection = array();
  27.  
  28. while($post = $stmt->fetchInto(new Post, "p")) {
  29. $post->author = $stmt->fetchIntoFromLastRow(new User, "u");
  30. $post_collection[] = $post;
  31. }
  32.  
  33. // Usage
  34. foreach($post_collection as $post) {
  35. echo $post->author->getName();
  36. }
  37.  
  38. class User {
  39. public
  40. $first_name,
  41. $last_name
  42. ;
  43. //etc...
  44. }
  45.  
  46. $db = DB::getInstance();
  47. $db->setFetchTableNames( 1 );
  48. $sql = $db->select(//etc...
  49.  
  50. while( $post = $stmt->fetchInto( new Post(), 'p' ) ) {
  51. $post->author = $stmt->fetchIntoFromLastRow( new User(), 'u' );
  52. echo $post->author->getName();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement