锂电mongodb模型之间的关系

发布于 2024-11-14 07:54:17 字数 1366 浏览 9 评论 0原文

我将锂与 mongodb 一起使用,我想知道下面的模型如何从 Posts::find('all'); 获取用户的数据询问?

我必须做两次查询吗?

感谢您的帮助!

    <?php
    namespace app\models;

    class Posts extends \lithium\data\Model {

        protected $_schema = array(
            '_id'           =>  array('type' => 'id'),
            'name'          =>  array('type' => 'string', 'null' => false),
            'description'   =>  array('type' => 'string', 'null' => false),
            'created'       =>  array('type' => 'datetime'),
            'updated'       =>  array('type' => 'datetime'),
            'user_id'       =>  array('type' => 'integer')
        );

        protected $_meta = array(
            'key' => '_id',
        );

        public $belongsTo = array('Users');


    }
    ?>


<?php
namespace app\models;

class Users extends \lithium\data\Model {

    public $hasMany = array('Posts');

    public $validates = array(
        'name' => 'Please enter a name',
    );

    protected $_schema = array(
        '_id'       =>  array('type' => 'id'),
        'name'      =>  array('type' => 'string', 'null' => false),
        'slug'      =>  array('type' => 'string', 'null' => false),
        'created'   =>  array('type' => 'datetime', 'null' => false),
    );

}
?>

I'm using lithium with mongodb and I would like to know with my models below how I could get the data of the user from Posts::find('all'); query?

Do I have to do two queries?

Thanks for your help!

    <?php
    namespace app\models;

    class Posts extends \lithium\data\Model {

        protected $_schema = array(
            '_id'           =>  array('type' => 'id'),
            'name'          =>  array('type' => 'string', 'null' => false),
            'description'   =>  array('type' => 'string', 'null' => false),
            'created'       =>  array('type' => 'datetime'),
            'updated'       =>  array('type' => 'datetime'),
            'user_id'       =>  array('type' => 'integer')
        );

        protected $_meta = array(
            'key' => '_id',
        );

        public $belongsTo = array('Users');


    }
    ?>


<?php
namespace app\models;

class Users extends \lithium\data\Model {

    public $hasMany = array('Posts');

    public $validates = array(
        'name' => 'Please enter a name',
    );

    protected $_schema = array(
        '_id'       =>  array('type' => 'id'),
        'name'      =>  array('type' => 'string', 'null' => false),
        'slug'      =>  array('type' => 'string', 'null' => false),
        'created'   =>  array('type' => 'datetime', 'null' => false),
    );

}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-11-21 07:54:17

目前关系仅存在于 MySQL 和 SQLite3 等关系数据库中。因此,您需要进行两次查询才能获取所需的数据。我们现在正在努力添加对基于文档的数据库的关系的支持,但目前还没有时间表。

您可以在帖子结果上使用 Set::extract 来提取所有用户 ID,然后使用该结果对用户进行单个查询 - 因此,您可以从帖子中执行 $userIDs = Set::extract('/ posts/user_id', $posts->data());然后 User::find('all', array('conditions' => array('_id' => $userIDs)));

希望这有帮助。

编辑:您可以在这里找到 set::extract 信息: http://li3.我/docs/lithium/util/Set::extract()

Currently relationships only exist for relational databases like MySQL and SQLite3. As such you'll need to make two queries to get the data you want. We're working on adding support for relationships for document based databases now however there currently is no timeframe on that.

You can use Set::extract on your result from posts to pull all of the user id's out then use the results from that to make a single query from users - so from posts you could do $userIDs = Set::extract('/posts/user_id', $posts->data()); then User::find('all', array('conditions' => array('_id' => $userIDs)));

hope this helps.

edit: You can find set::extract information here: http://li3.me/docs/lithium/util/Set::extract()

怪我鬧 2024-11-21 07:54:17

我必须做两个查询吗?

这将取决于您的架构。

案例 #1

如果 UsersPosts 是两个不同的集合,那么您将需要两个不同的查询。

案例 #2

如果 Users 是顶级对象,并且 Posts “属于”Users 那么你会做一些事情相当于 db.users.find({ posts : {$exists:true} })。

我不是 100% 清楚 Lithium 如何处理这个问题。我找不到一个简单的例子来说明锂是在做#1还是#2。

Do I have to do two queries?

This will depend on your schema.

Case #1

If Users and Posts are two different collections then you will need two different queries.

Case #2

If Users is the top level object and Posts "belongs to" Users then you would do something equivalent to db.users.find({ posts : {$exists:true} }).

I'm not 100% clear on how Lithium handles this. I cannot find a simple example of whether Lithium is doing #1 or #2.

游魂 2024-11-21 07:54:17

正如 Howard3 所说,目前 MongoDB 没有关系支持,因此“属于”也不起作用。

实际的决定取决于您的应用程序,但我认为它将是某种形式的博客(用户和帖子)。根据 MongoDB 模式设计最佳实践,我将它们放在单独的集合中,因为它们是“第一级集合”。更适合嵌入文档的是帖子和评论。

另外,当您使用最新的 master 时,您不必定义“关键”内容。您现在可以编写一个自定义查找方法,可以轻松地将其替换为更通用的解决方案当核心中的关系支持完成时。

如果您需要更多交互式帮助,请访问 freenode 上的#li3。

As Howard3 said, there is currently no relationship support for MongoDB and as a result "belongs to" won't work either.

The actual decision depends on your application, but i assume that its going to be some form of a blog (users and posts). According to MongoDB schema design best practices, i'd put both in separate collections because they are "first level collections". a better fit for embedded documents would be posts and comments.

Also, you don't have to define the 'key' stuff when you're on latest master. You can write a custom find method for now that could be easily swapped to the more generic solution when relation support is finished in the core.

if you need more interactive help, visit #li3 on freenode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文