summaryrefslogtreecommitdiff
path: root/docs/models.rst
blob: d72e27d7d664868eaa88527dd53a7fb75eb1d7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Models
======

Getting data from objects
~~~~~~~~~~~~~~~~~~~~~~~~~

Once you've got a set of records (objects) back from a query, you can
access properties on those objects (the values stored in the columns in
its corresponding table) in two ways: by using the ``get`` method, or
simply by accessing the property on the object directly:

.. code-block:: php

    <?php
    $person = ORM::for_table('person')->find_one(5);

    // The following two forms are equivalent
    $name = $person->get('name');
    $name = $person->name;

You can also get the all the data wrapped by an ORM instance using the
``as_array`` method. This will return an associative array mapping
column names (keys) to their values.

The ``as_array`` method takes column names as optional arguments. If one
or more of these arguments is supplied, only matching column names will
be returned.

.. code-block:: php

    <?php
    $person = ORM::for_table('person')->create();

    $person->first_name = 'Fred';
    $person->surname = 'Bloggs';
    $person->age = 50;

    // Returns array('first_name' => 'Fred', 'surname' => 'Bloggs', 'age' => 50)
    $data = $person->as_array();

    // Returns array('first_name' => 'Fred', 'age' => 50)
    $data = $person->as_array('first_name', 'age');

Updating records
~~~~~~~~~~~~~~~~

To update the database, change one or more of the properties of the
object, then call the ``save`` method to commit the changes to the
database. Again, you can change the values of the object's properties
either by using the ``set`` method or by setting the value of the
property directly. By using the ``set`` method it is also possible to
update multiple properties at once, by passing in an associative array:

.. code-block:: php

    <?php
    $person = ORM::for_table('person')->find_one(5);

    // The following two forms are equivalent
    $person->set('name', 'Bob Smith');
    $person->age = 20;

    // This is equivalent to the above two assignments
    $person->set(array(
        'name' => 'Bob Smith',
        'age'  => 20
    ));

    // Syncronise the object with the database
    $person->save();

Properties containing expressions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It is possible to set properties on the model that contain database
expressions using the ``set_expr`` method.

.. code-block:: php

    <?php
    $person = ORM::for_table('person')->find_one(5);
    $person->set('name', 'Bob Smith');
    $person->age = 20;
    $person->set_expr('updated', 'NOW()');
    $person->save();

The ``updated`` column's value will be inserted into query in its raw
form therefore allowing the database to execute any functions referenced
- such as ``NOW()`` in this case.

Creating new records
~~~~~~~~~~~~~~~~~~~~

To add a new record, you need to first create an "empty" object
instance. You then set values on the object as normal, and save it.

.. code-block:: php

    <?php
    $person = ORM::for_table('person')->create();

    $person->name = 'Joe Bloggs';
    $person->age = 40;

    $person->save();

After the object has been saved, you can call its ``id()`` method to
find the autogenerated primary key value that the database assigned to
it.

Properties containing expressions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It is possible to set properties on the model that contain database
expressions using the ``set_expr`` method.

.. code-block:: php

    <?php
    $person = ORM::for_table('person')->create();
    $person->set('name', 'Bob Smith');
    $person->age = 20;
    $person->set_expr('added', 'NOW()');
    $person->save();

The ``added`` column's value will be inserted into query in its raw form
therefore allowing the database to execute any functions referenced -
such as ``NOW()`` in this case.

Checking whether a property has been modified
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To check whether a property has been changed since the object was
created (or last saved), call the ``is_dirty`` method:

.. code-block:: php

    <?php
    $name_has_changed = $person->is_dirty('name'); // Returns true or false

Deleting records
~~~~~~~~~~~~~~~~

To delete an object from the database, simply call its ``delete``
method.

.. code-block:: php

    <?php
    $person = ORM::for_table('person')->find_one(5);
    $person->delete();

To delete more than one object from the database, build a query:

.. code-block:: php

    <?php
    $person = ORM::for_table('person')
        ->where_equal('zipcode', 55555)
        ->delete_many();