3

Example: I have following two classes for tables in database.

            class Post extends Model {}
            class User extends Model {
               public function posts() {
                   return $this->has_many('Post'); // Note we use the model name literally - not a    pluralised version
               }
            }

Now I want to quert all users and their associated posts in one variable. something like that:

            $user = Model::factory('User')->find_many();
            // Find the posts associated with the user
            $posts = $user->posts()->find_many();

I want to get the result in $posts variable.

Hikmat
  • 450
  • 4
  • 19

1 Answers1

3

I have solved this issue my self

            $user = Model::factory('User')->find_many();
            // Find the posts associated with the user
            //$posts = $user->posts()->find_many();

I looped through $user and passed each user id to $user->posts()->find_one();

sudo code is here:

            foreach($user as $usr)
            {
               $posts = $user->posts()->find_one($usr->Id);
            }
Hikmat
  • 450
  • 4
  • 19
  • You must add your solution! Maybe there are other programmers who may have the same problem and needs your solution – Mihai Matei Mar 10 '15 at 07:19