Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User extends stdClass {
- function getName() {
- return $this->first_name . " ". $this->last_name;
- }
- }
- class Post extends stdClass {
- /**
- * @var User
- */
- public $author;
- }
- DB::getInstance()->setFetchTableNames(1);
- $sql = DB::getInstance()
- ->select("p.*, u.*")
- ->from("posts p")
- ->join("INNER JOIN users u USING(user_id)")
- ->where("u.user_id = ?", $user_id)
- ->orderBy("p.title");
- $stmt = $sql->execute();
- /* @var Post[] $post_collection */
- $post_collection = array();
- while($post = $stmt->fetchInto(new Post, "p")) {
- $post->author = $stmt->fetchIntoFromLastRow(new User, "u");
- $post_collection[] = $post;
- }
- // Usage
- foreach($post_collection as $post) {
- echo $post->author->getName();
- }
- class User {
- public
- $first_name,
- $last_name
- ;
- //etc...
- }
- $db = DB::getInstance();
- $db->setFetchTableNames( 1 );
- $sql = $db->select(//etc...
- while( $post = $stmt->fetchInto( new Post(), 'p' ) ) {
- $post->author = $stmt->fetchIntoFromLastRow( new User(), 'u' );
- echo $post->author->getName();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement