2

I am using Paris (which is built on top of Idiorm).

I have the following Model classes (example inspired from the documentation on github):

<?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
    }
}

So now I can do the following (works well):

// 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();

But I'd like to do the following aswell:

$posts = Model::factory('Post')->find_many();
foreach ($posts as $post) {
     echo($post->user()->find_one()->username); // generates a query each iteration
}

Unfortunately this creates a query for each iteration. Is there a way to tell Paris or Idiorm to take the associated information in the first find_many query ?

How are you supposed to retrieve information with Paris to minimize the numbers of query ? I'd like not to have to manually specify the join condition (this is the reason why I am using Paris and not Idiorm)

2 Answers 2

4

I am the current maintainer of the Paris and Idiorm projects. Unfortunately what you describe here is a case for a custom query and not something that Paris was ever built to solve. The philosophy of Paris and Idiorm is to stick as close to the 80/20 principle as possible and your use case is in the 20.

It would be interesting to know how you ended up solving this problem.

1
  • 2
    So far, I only worked around the problem. On one project, I just switch to Rails. On the other project, I use a mixture of Idiorm and Paris, and all complex requests are done with joins in Idiorm. I really don't like mixing both Idiorm and paris, so I might come back and actually solve the problem. +1 for maintaining the project. Jan 15, 2013 at 16:27
0

I've had exactly the same problem, and ended up with this (a bit ugly) solution:

$posts = Model::factory('Post')...->limit($perpage)->find_many();
$user_ids = $user_lookup = array();

foreach($posts as $post) $user_ids[] = $post->user_id;

$users = Model::factory('User')->where_id_in($user_ids)->find_many();
foreach($users as $user) $user_lookup[$user->id] = $user;

Only 2 selects. And later in template:

{% for post in posts %}
   <h2>{{ post.title }}</h2>
   by author: {{ user_lookup[post.user_id].username }}
{% endfor %}

But it only works if you don't have hundreds of posts showing on one page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.