CodeIgniter 2真的很给力啊。现在模型支持load其他模型了。方便很多。代码说话:
db->where('email', $email)->get('users')->row(0);
}
}
db->where('user_id', $user_id)->get('comments')->result();
}
}
要在controller里这样调用:
load->model('comment');
$this->load->model('user');
// get the user and their fancy comments
$user = $this->user->get_by_email('john.crepezzi@gmail.com');
$comments = $this->comment->get_by_user_id($user->id);
现在方便了,我们可以这样:
db->where('email', $email)->get('users')->row(0, 'User');
}
function comments() {
$this->load->model('comment');
return $this->comment->get_by_user_id($user->id);
}
}
db->where('user_id', $user_id)->get('comments')->result('Comment');
}
}
完了这样调用:
load->model('user');
// get the user and their fancy comments
$user = $this->user->get_by_email('john.crepezzi@gmail.com');
$user->comments();
给力!