| 1 | 1 | 1; | |
| 2 | package OpenERP::OOM::Tutorial::Schema; | ||
| 3 | |||
| 4 | =pod | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | OpenERP::OOM::Tutorial::Schema - Setting up an OpenERP::OOM Schema | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | =head2 Object model | ||
| 13 | |||
| 14 | OpenERP::OOM uses an object model consisting of a Schema, Classes, and Objects. | ||
| 15 | These are organised as shown below, with methods to traverse up and down the tree. | ||
| 16 | |||
| 17 | +-------------------------------+ | ||
| 18 | | Schema | | ||
| 19 | | e.g. MyApp | | ||
| 20 | +-------------------------------+ | ||
| 21 | /|\ | | ||
| 22 | | class | ||
| 23 | schema | | ||
| 24 | | \|/ | ||
| 25 | +-------------------------------+ | ||
| 26 | | Class | | ||
| 27 | | e.g. MyApp::Class::Partner | | ||
| 28 | +-------------------------------+ | ||
| 29 | /|\ | | ||
| 30 | | search | ||
| 31 | class retrieve | ||
| 32 | | create | ||
| 33 | | | | ||
| 34 | | \|/ | ||
| 35 | +-------------------------------+ | ||
| 36 | | Object | | ||
| 37 | | e.g. MyApp::Object::Partner | | ||
| 38 | +-------------------------------+ | ||
| 39 | | | ||
| 40 | update | ||
| 41 | delete | ||
| 42 | | | ||
| 43 | \|/ | ||
| 44 | +-------------------------------+ | ||
| 45 | | OpenERP model | | ||
| 46 | | e.g res.partner | | ||
| 47 | +-------------------------------+ | ||
| 48 | |||
| 49 | =head2 Schema | ||
| 50 | |||
| 51 | The schema class extends the OpenERP::OOM::Schema module, i.e.: | ||
| 52 | |||
| 53 | package MyApp; | ||
| 54 | |||
| 55 | use Moose; | ||
| 56 | extends 'OpenERP::OOM::Schema'; | ||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | To create a new instance of your schema, pass the OpenERP connection details in | ||
| 61 | the call to C<new()>. | ||
| 62 | |||
| 63 | use MyApp; | ||
| 64 | |||
| 65 | my $schema = MyApp->new( | ||
| 66 | openerp_connect => { | ||
| 67 | host => 'localhost', | ||
| 68 | dbname => 'openerp_db', | ||
| 69 | username => 'admin', | ||
| 70 | password => 'admin', | ||
| 71 | } | ||
| 72 | ); | ||
| 73 | |||
| 74 | =head2 Classes | ||
| 75 | |||
| 76 | A typical class definition is as follows: | ||
| 77 | |||
| 78 | package MyApp::Class::Partner; | ||
| 79 | use OpenERP::OOM::Class; | ||
| 80 | |||
| 81 | object_type 'MyApp::Object::Company'; | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | From your schema, you can then access classes. | ||
| 86 | |||
| 87 | my $class = $schema->class('Partner'); | ||
| 88 | |||
| 89 | The class provides methods to search, retrieve, and create objects. | ||
| 90 | |||
| 91 | =head2 Objects | ||
| 92 | |||
| 93 | package MyApp::Object::Partner; | ||
| 94 | use OpenERP::OOM::Object; | ||
| 95 | |||
| 96 | openerp_model 'res.partner'; | ||
| 97 | |||
| 98 | has 'name' => (isa=>'Str', is=>'rw'); | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | Objects are created and retrieved from the class: | ||
| 103 | |||
| 104 | my $partner = $schema->class('Partner')->create({ | ||
| 105 | name => 'My Partner', | ||
| 106 | }); | ||
| 107 | |||
| 108 | foreach my $partner ($schema->class->('Partner')->search) { | ||
| 109 | say $partner->name; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | __END__ |