The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mandel::Relationship::BelongsTo - A document is owned by another mongodb document

DESCRIPTION

Mandel::Relationship::BelongsTo is a class used to describe the relationship between one document that belongs to another document. The connection between the documents is described in the database using DBRef.

DATABASE STRUCTURE

A "cat" that belongs to a "person" will look like this in the database:

mongodb# db.persons.find();
{ "_id" : ObjectId("5352abb0c5483e591a010000") }

mongodb# db.cats.find({ "person.$id": ObjectId("5352abb0c5483e591a010000") })
{
  "_id" : ObjectId("5352abb0c5483e591a020000"),
  "person" : DBRef("persons", ObjectId("5352abb0c5483e591a010000"))
}

SYNOPSIS

Using DSL

package MyModel::Cat;
use Mandel::Document;
belongs_to owner => 'MyModel::Person';

Using object oriented interface

MyModel::Cat
  ->model
  ->relationship(belongs_to => owner => 'MyModel::Person');

See also "relationship" in Mandel::Model.

Methods generated

# non-blocking set
$cat = MyModel::Cat->new->owner(\%args, sub {
         my($cat, $err, $person_obj) = @_;
         # ...
       });

$cat = MyModel::Cat->new->owner($person_obj, sub {
         my($cat, $err, $person_obj) = @_;
         # ...
       });

# non-blocking get
$cat = MyModel::Cat->new->owner(sub {
         my($cat, $err, $person_obj) = @_;
         # ...
       });


# blocking set
$person_obj = MyModel::Cat->new->owner(\%args);
$person_obj = MyModel::Cat->new->owner($person_obj);
$person_obj = MyModel::Cat->new->owner($bson_oid);

# blocking get
$person = MyModel::Cat->new->owner;

ATTRIBUTES

Mandel::Relationship::BelongsTo inherits all attributes from Mandel::Relationship and implements the following new ones.

foreign_field

The name of the field in this class which hold the "_id" to the related doc.

METHODS

Mandel::Relationship::BelongsTo inherits all methods from Mandel::Relationship and implements the following new ones.

monkey_patch

Add methods to "document_class" in Mandel::Relationship.

SEE ALSO

Mojolicious, Mango, Mandel::Relationship

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org