Вячеслав Тихановский > ObjectDB-0.990103 > ObjectDB::Cookbook

Download:
ObjectDB-0.990103.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Source  

NAME ^

ObjectDB::Cookbook - Cooking with ObjectDB

RECIPIES ^

Single object manipulations

Create

    # Create new article with title 'foo'
    my $article = Article->new(title => 'foo');
    $article->create;

Update

    # Update loaded article
    $article->column(title => 'bar');
    $article->update;

Delete

    # Delete loaded article
    $article->delete;

Load

    # Load article providing a primary key 'id'
    my $article = Article->new(id => 1);
    $article->load;

Multiple objects manipulations

Find

    # Find articles with title 'foo'
    my $articles = Article->find(where => [title => 'foo']);

    # Find article with title 'foo'
    my $articles = Article->find(where => [title => 'foo'], single => 1);

    # Find articles using paging
    my $articles = Article->find(page => 1, page_size => 10);

Update

    # Update articles titles to 'bar' where titles are 'foo'
    Author->update(set => {title => 'bar'}, where => [title => 'foo']);

Delete

    # Delete all articles
    Article->delete;

    # Delete articles where title is 'foo'
    Article->delete(where => [title => 'foo']);

Count

    # Count all articles
    my $total = Article->count;

    # Count articles where title is 'foo'
    my $total_with_title_foo = Article->count(where => [title => 'foo']);

Relationships

Manipulations on related objects

    $article->find_related('comments', where => [author => 'foo']);
    $article->delete_related('comments', where => [author => 'foo']);
    $article->count_related('comments', where => [author => 'foo']);
    $article->update_related(
        'comments',
        set   => {author => 'bar'},
        where => [author => 'foo']
    );
    $article->set_related('tags' => {name => 'foo'});

Preloading related objects

    my $article = Article->find(with => 'tags');

Preloading related objects on demand

    my $tags = $article->load_related('tags');

Accessing preloaded related objects

    my $tags = $article->related('tags');

Deep nested relationships

    # Find author whose articles are in 'foo' category
    my $authors = Author->find(where => ['articles.category.title' => 'foo']);

Real world examples

AUTHOR ^

Viacheslav Tykhanovskyi, vti@cpan.org.

COPYRIGHT ^

Copyright (C) 2009, Viacheslav Tykhanovskyi.

This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10.

syntax highlighting: