php - 如何使用 Idiorm 和 Paris 仅通过一个查询从关联表中检索信息?

标签 php orm idiorm

我正在使用Paris (它构建在 Idiorm 之上)。

我有以下模型类(示例灵感来自 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
    }
}

现在我可以执行以下操作(效果很好):

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

不幸的是,这会为每次迭代创建一个查询。有没有办法告诉 Paris 或 Idiorm 在第一个 find_many 查询中获取关联信息?

您应该如何通过 Paris 检索信息以尽量减少查询次数?我不想手动指定连接条件(这就是我使用 Paris 而不是 Idiorm 的原因)

最佳答案

我是 Paris 和 Idiorm 项目的当前维护者。不幸的是,您在这里描述的是自定义查询的情况,而不是巴黎旨在解决的问题。 Paris 和 Idiorm 的理念是尽可能遵循 80/20 原则,并且您的用例位于 20 中。

了解您最终如何解决这个问题会很有趣。

关于php - 如何使用 Idiorm 和 Paris 仅通过一个查询从关联表中检索信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12597197/

相关文章:

javascript - 通过 Highcharts 从 MySQL 获取 JSON 编码数据时遇到问题

PHP/MySQL 多列排序

mongodb - mongodb是否有 "ER Diagram"可用?

php - MySQL - ORM Idiorm - 防止 SQL 注入(inject)

php - 如何在 PHP 的惯用 SQL 查询中使用 OR?

php - 使用 idiorm 从 mysql 获取以特殊字符开头的数据

php - 我的 str_replace 代码不起作用

php - 创建或更新 - Laravel 4.1

node.js - 更改模型后如何创建 Sequelize cli db 迁移

python - 使用 SQLAlchemy 计算结果集中包含的值实例