Skip to main content

References Relationships

Of course, your database collections are probably related to one another. For example, a blog post may have many comments, or an order could be related to the user who placed it. Mongolid makes managing and working with these relationships easy. MongoDB and Mongolid in short supports four types of relationships:

note

MongoDB relationships doesn't work like in a Relational database. In MongoDB, data modeling decisions involve determining how to structure the documents to model the data effectively. The primary decision is whether to embed or to use references. See MongoDB - Data Modeling Decisions for more information on this subject.


References One

In Mongolid a reference is made by storing the _id of the referenced object.

Referencing provides more flexibility than embedding; however, to resolve the references, client-side applications must issue follow-up queries. In other words, using references requires more roundtrips to the server.

In general, use references when embedding would result in duplication of data and would not provide sufficient read performance advantages to outweigh the implications of the duplication. Read MongoDB - Relationships with Document References to learn more how to take advantage of referencing in MongoDB.

caution

MongoDB relationships doesn't work like in a Relational database. In MongoDB, data modeling decisions involve determining how to structure the documents to model the data effectively. If you try to create references between documents like you would do in a relational database you will end up with "n+1 problem" and poor performance.

Defining A References One Relation

    class Post extends \Mongolid\Model\AbstractModel {
protected $collection = 'posts';

public function author()
{
return $this->referencesOne(User::class, 'author');
}
}

class User extends \Mongolid\Model\AbstractModel {
protected $collection = 'users';
}

The first argument passed to the referencesOne method is the name of the related model, the second argument is the attribute where the referenced model _id will be stored. Once the relationship is defined, we may retrieve it using the following method:

    $user = Post::find('4af9f23d8ead0e1d32000000')->author();
info

This statement will perform the following:

  • Query for the post with the _id '4af9f23d8ead0e1d32000000'
  • Query for the user with the _id equals to the author attribute of the post
  • Return that object

In order to set a reference to a document, simply set the attribute used in the relationship to the reference's _id or use the attachment method, or it's alias. For example:

    // The object that will be embedded
$user = new User();
$user->name = 'John';
$user->save() // This will populate the $user->_id

// The object that will contain the user
$post = Post::first('4af9f23d8ead0e1d32000000');

// This method will attach the $phone _id into the phone attribute of the user
$post->attach('author', $user);

// This is an alias to the method called above.
$post->attachToAuthor($user);

// This will also work
$post->author = $user->_id;

$post->save();

$post->author(); // Will return a User object
info

When using Mongolid models you will need to call the save() method after embedding or attaching objects. The changes will only persist after you call the 'save()' method.

References Many

In Mongolid a References Many is made by storing the _ids of the referenced objects.

Referencing provides more flexibility than embedding; however, to resolve the references, client-side applications must issue follow-up queries. In other words, using references requires more roundtrips to the server.

In general, use references when embedding would result in duplication of data and would not provide sufficient read performance advantages to outweigh the implications of the duplication. Read MongoDB - Relationships with Document References to learn more how to take advantage of referencing in MongoDB.

Defining A References Many Relation

    class User extends \Mongolid\Model\AbstractModel {
protected $collection = 'users';

public function questions()
{
return $this->referencesMany(Question::class, 'questions');
}

}

class Question extends \Mongolid\Model\AbstractModel {
protected $collection = 'questions';
}

The first argument passed to the referencesMany method is the name of the related model, the second argument is the attribute where the _ids will be stored. Once the relationship is defined, we may retrieve it using the following method:

    $posts = User::find('4af9f23d8ead0e1d32000000')->posts();
info

This statement will perform the following:

  • Query for the user with the _id '4af9f23d8ead0e1d32000000'
  • Query for all the posts with the _id in the user's posts attribute
  • Return the Mongolid\Cursor\Cursor with the related posts

In order to set a reference to a document use the attachment method, or it's alias. For example:

    $postA = new Post();
$postA->title = 'Nice post';

$postB = new Post();
$postB->title = 'Nicer post';

$user = User::first('4af9f23d8ead0e1d32000000');

// Both ways work
$user->attachToPosts($postA);
$user->attach('posts', $postB);

$user->save();
info

When using Mongolid models you will need to call the save() method after embedding or attaching objects. The changes will only persist after you call the 'save()' method.

tip

You can use detach method with the referenced object, or it's _id in order to remove a single reference.