Li-Mongodb relationship between models

I use lithium with mongodb, and I would like to learn with my models below how I can get user data from Posts :: find ('all'); request?

Should I fulfill two requests?

Thank you 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), ); } ?> 
+4
source share
3 answers

Relationships currently exist only for relational databases such as MySQL and SQLite3. Thus, you will need to make two queries to get the data you need. We are working on adding relationship support for document-based databases, but there is currently no time frame.

You can use Set :: extract in the results from messages to pull out the entire user ID, and then use the results to make a single request from users - therefore, from the messages you could make $ userIDs = Set :: extract ('/ posts / user_id ', $ posts-> data ()); then User :: find ('all', array ('conditions' => array ('_ id' => $ userID)));

hope this helps.

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

+4
source

Should I fulfill two requests?

It will depend on your scheme.

Case No. 1

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

Case No. 2

If Users is a top-level object and Posts "owned" by Users , you should do something equivalent to db.users.find({ posts : {$exists:true} }) .

I do not 100% understand how lithium deals with this. I cannot find a simple example of whether Lithium does # 1 or # 2.

+1
source

As Howard3 said, MongoDB support is currently not supported by relationships, and as a result, “owned” will not work either.

The actual solution depends on your application, but I assume it will be some form of blog (users and posts). In accordance with MongoDB schema design best practices, I would put both in different collections because they are “first level collections”. better suited for attached documents, there will be posts and comments.

In addition, you do not need to define a “key” when you are on the last host. You can write a custom search method , which can now be easily swapped for a more general solution when the relationship support is completed in the kernel.

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

+1
source

All Articles