Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Post extends Model {
- public function user() {
- return $this->belongs_to('User');
- }
- }
- class User extends Model {
- public function posts() {
- return $this->has_many('Post'); // Note we use the model name literally - not a pluralised version
- }
- }
- // Select a particular user from the database
- $user = Model::factory('User')->find_one($user_id);
- // Find the posts associated with the user
- $posts = $user->posts()->find_many();
- // Select a particular post from the database
- $post = Model::factory('Post')->find_one($post_id);
- // Find the user associated with the post
- $user = $post->user()->find_one();
- $posts = Model::factory('Post')->find_many();
- foreach ($posts as $post) {
- echo($post->user()->find_one()->username); // generates a query each iteration
- }
Add Comment
Please, Sign In to add comment