2011-02-07 07:48:44 +01:00
|
|
|
# Datamodel
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
SilverStripe uses an [object-relational model](http://en.wikipedia.org/wiki/Object-relational_model)
|
|
|
|
that assumes the following connections:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
* Each database-table maps to a PHP class
|
|
|
|
* Each database-row maps to a PHP object
|
|
|
|
* Each database-column maps to a property on a PHP object
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
All data tables in SilverStripe are defined as subclasses of `[api:DataObject]`.
|
|
|
|
|
|
|
|
Inheritance is supported in the data model: separate tables will be linked
|
|
|
|
together, the data spread across these tables. The mapping and saving/loading
|
|
|
|
logic is handled by SilverStripe, you don't need to worry about writing SQL most
|
|
|
|
of the time.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
Most of the ORM customizations are possible through [PHP5 Object
|
2013-06-08 05:14:53 +02:00
|
|
|
Overloading](http://www.onlamp.com/pub/a/php/2005/06/16/overloading.html)
|
|
|
|
handled in the `[api:Object]`-class.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
See [database-structure](/reference/database-structure) for in-depth information
|
|
|
|
on the database-schema and the ["sql queries" topic](/reference/sqlquery) in
|
|
|
|
case you need to drop down to the bare metal.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
## Generating the Database Schema
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
The SilverStripe database-schema is generated automatically by visiting the URL.
|
2013-10-23 21:24:52 +02:00
|
|
|
`http://localhost/dev/build`
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
<div class="notice" markdown='1'>
|
2013-01-22 14:29:58 +01:00
|
|
|
Note: You need to be logged in as an administrator to perform this command,
|
2013-06-08 05:14:53 +02:00
|
|
|
unless your site is in "[dev mode](/topics/debugging)", or the command is run
|
|
|
|
through CLI.
|
2011-03-08 22:05:51 +01:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
## Querying Data
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Every query to data starts with a `DataList::create(<class>)` or `<class>::get()`
|
|
|
|
call. For example, this query would return all of the `Member` objects:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get();
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
The ORM uses a "fluent" syntax, where you specify a query by chaining together
|
|
|
|
different methods. Two common methods are `filter()` and `sort()`:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2013-06-08 05:14:53 +02:00
|
|
|
$members = Member::get()->filter(array(
|
|
|
|
'FirstName' => 'Sam'
|
|
|
|
))->sort('Surname');
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Those of you who know a bit about SQL might be thinking "it looks like you're
|
|
|
|
querying all members, and then filtering to those with a first name of 'Sam'.
|
|
|
|
|
|
|
|
Isn't this very slow?" Is isn't, because the ORM doesn't actually execute the
|
|
|
|
SQL query until you iterate on the result with a `foreach()` or `<% loop %>`.
|
|
|
|
The ORM is smart enough to generate a single efficient query at the last moment
|
|
|
|
in time without needing to post process the result set in PHP. In MySQL the
|
|
|
|
query generated by the ORM may look something like this for the previous query.
|
2012-08-03 07:00:43 +02:00
|
|
|
|
|
|
|
:::
|
|
|
|
SELECT * FROM Member WHERE FirstName = 'Sam' ORDER BY Surname
|
|
|
|
|
|
|
|
An example of the query process in action:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2011-04-04 02:18:50 +02:00
|
|
|
// The SQL query isn't executed here...
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get();
|
2011-04-04 02:18:50 +02:00
|
|
|
// ...or here
|
|
|
|
$members = $members->filter(array('FirstName' => 'Sam'));
|
|
|
|
// ...or even here
|
|
|
|
$members = $members->sort('Surname');
|
|
|
|
|
|
|
|
// *This* is where the query is executed
|
|
|
|
foreach($members as $member) {
|
|
|
|
echo "<p>$member->FirstName $member->Surname</p>";
|
|
|
|
}
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
This also means that getting the count of a list of objects will be done with a
|
|
|
|
single, efficient query.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2011-04-04 02:18:50 +02:00
|
|
|
:::php
|
2013-06-08 05:14:53 +02:00
|
|
|
$members = Member::get()->filter(array(
|
|
|
|
'FirstName' => 'Sam'
|
|
|
|
))->sort('Surname');
|
2012-08-03 07:00:43 +02:00
|
|
|
|
|
|
|
// This will create an single SELECT COUNT query similar to -
|
|
|
|
// SELECT COUNT(*) FROM Members WHERE FirstName = 'Sam'
|
2011-04-04 02:18:50 +02:00
|
|
|
echo $members->Count();
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2011-04-04 02:18:50 +02:00
|
|
|
### Returning a single DataObject
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
There are a couple of ways of getting a single DataObject from the ORM. If you
|
|
|
|
know the ID number of the object, you can use `byID($id)`:
|
2011-04-04 02:18:50 +02:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$member = Member::get()->byID(5);
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
If you have constructed a query that you know should return a single record, you
|
|
|
|
can call `First()`:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2013-06-08 05:14:53 +02:00
|
|
|
$member = Member::get()->filter(array(
|
|
|
|
'FirstName' => 'Sam', 'Surname' => 'Minnee'
|
|
|
|
))->First();
|
2011-04-04 02:18:50 +02:00
|
|
|
|
|
|
|
|
2011-12-08 22:09:48 +01:00
|
|
|
### Sort
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Quite often you would like to sort a list. Doing this on a list could be done in
|
|
|
|
a few ways.
|
2011-12-08 22:09:48 +01:00
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
If would like to sort the list by `FirstName` in a ascending way (from A to Z).
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
:::php
|
2013-06-08 05:14:53 +02:00
|
|
|
$members = Member::get()->sort('FirstName', 'ASC'); // ASC or DESC
|
|
|
|
$members = Member::get()->sort('FirstName'); // Ascending is implied
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
To reverse the sort
|
|
|
|
|
|
|
|
:::php
|
2013-06-08 05:14:53 +02:00
|
|
|
$members = Member::get()->sort('FirstName', 'DESC');
|
|
|
|
|
|
|
|
// or..
|
|
|
|
$members = Member::get()->sort('FirstName', 'ASC')->reverse();
|
2011-12-08 22:09:48 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
However you might have several entries with the same `FirstName` and would like
|
|
|
|
to sort them by `FirstName` and `LastName`
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$member = Member::get()->sort(array(
|
2011-12-08 22:09:48 +01:00
|
|
|
'FirstName' => 'ASC',
|
|
|
|
'LastName'=>'ASC'
|
|
|
|
));
|
|
|
|
|
2012-08-03 07:00:43 +02:00
|
|
|
You can also sort randomly
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$member = Member::get()->sort('RAND()')
|
|
|
|
|
2011-12-08 22:09:48 +01:00
|
|
|
### Filter
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
As you might expect, the `filter()` method filters the list of objects that gets
|
|
|
|
returned. The previous example included this filter, which returns all Members
|
|
|
|
with a first name of "Sam".
|
2011-04-04 02:18:50 +02:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->filter(array('FirstName' => 'Sam'));
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
In SilverStripe 2, we would have passed `"\"FirstName\" = 'Sam'` to make this
|
|
|
|
query. Now, we pass an array, `array('FirstName' => 'Sam')`, to minimize the
|
|
|
|
risk of SQL injection bugs. The format of this array follows a few rules:
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
* Each element of the array specifies a filter. You can specify as many
|
|
|
|
filters as you like, and they **all** must be true.
|
2011-04-04 02:18:50 +02:00
|
|
|
* The key in the filter corresponds to the field that you want to filter by.
|
|
|
|
* The value in the filter corresponds to the value that you want to filter to.
|
|
|
|
|
|
|
|
So, this would return only those members called "Sam Minnée".
|
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->filter(array(
|
2011-04-04 02:18:50 +02:00
|
|
|
'FirstName' => 'Sam',
|
|
|
|
'Surname' => 'Minnée',
|
|
|
|
));
|
|
|
|
|
2012-08-03 07:00:43 +02:00
|
|
|
There is also a short hand way of getting Members with the FirstName of Sam.
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->filter('FirstName', 'Sam');
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
Or if you want to find both Sam and Sig.
|
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->filter(
|
2011-12-08 22:09:48 +01:00
|
|
|
'FirstName', array('Sam', 'Sig')
|
|
|
|
);
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Then there is the most complex task when you want to find Sam and Sig that has
|
|
|
|
either Age 17 or 74.
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->filter(array(
|
2011-12-08 22:09:48 +01:00
|
|
|
'FirstName' => array('Sam', 'Sig'),
|
|
|
|
'Age' => array(17, 74)
|
|
|
|
));
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2012-10-12 12:24:03 +02:00
|
|
|
// SQL: WHERE ("FirstName" IN ('Sam', 'Sig) AND "Age" IN ('17', '74))
|
2011-12-08 22:09:48 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
In case you want to match multiple criteria non-exclusively (with an "OR"
|
|
|
|
disjunctive),use the `filterAny()` method instead:
|
2011-12-08 22:09:48 +01:00
|
|
|
|
2012-10-12 12:24:03 +02:00
|
|
|
:::php
|
|
|
|
$members = Member::get()->filterAny(array(
|
|
|
|
'FirstName' => 'Sam',
|
|
|
|
'Age' => 17,
|
|
|
|
));
|
|
|
|
// SQL: WHERE ("FirstName" = 'Sam' OR "Age" = '17')
|
|
|
|
|
|
|
|
You can also combine both conjunctive ("AND") and disjunctive ("OR") statements.
|
2011-12-08 22:09:48 +01:00
|
|
|
|
2012-10-12 12:24:03 +02:00
|
|
|
:::php
|
|
|
|
$members = Member::get()
|
|
|
|
->filter(array(
|
|
|
|
'LastName' => 'Minnée'
|
|
|
|
))
|
|
|
|
->filterAny(array(
|
|
|
|
'FirstName' => 'Sam',
|
|
|
|
'Age' => 17,
|
|
|
|
));
|
2013-06-08 05:14:53 +02:00
|
|
|
// WHERE ("LastName" = 'Minnée' AND ("FirstName" = 'Sam' OR "Age" = '17'))
|
2013-10-30 14:08:55 +01:00
|
|
|
|
|
|
|
### Filter with PHP / filterByCallback
|
|
|
|
|
|
|
|
It is also possible to filter by a PHP callback, however this will force the
|
|
|
|
data model to fetch all records and loop them in PHP, thus `filter()` or `filterAny()`
|
|
|
|
are to be preferred over `filterByCallback()`.
|
|
|
|
Please note that because `filterByCallback()` has to run in PHP, it will always return
|
|
|
|
an `ArrayList` (even if called on a `DataList`, this however might change in future).
|
|
|
|
The first parameter to the callback is the item, the second parameter is the list itself.
|
|
|
|
The callback will run once for each record, if the callback returns true, this record
|
|
|
|
will be added to the list of returned items.
|
|
|
|
The below example will get all Members that have an expired or not encrypted password.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$membersWithBadPassword = Member::get()->filterByCallback(function($item, $list) {
|
|
|
|
if ($item->isPasswordExpired() || $item->PasswordEncryption = 'none') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
### Exclude
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
The `exclude()` method is the opposite to the filter in that it removes entries
|
|
|
|
from a list.
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
If we would like to remove all members from the list with the FirstName of Sam.
|
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->exclude('FirstName', 'Sam');
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
Remove both Sam and Sig is as easy as.
|
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->exclude('FirstName', array('Sam','Sig'));
|
2011-12-08 22:09:48 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
As you can see it follows the same pattern as filter, so for removing only Sam
|
|
|
|
Minnée from the list:
|
2011-12-08 22:09:48 +01:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->exclude(array(
|
2011-12-08 22:09:48 +01:00
|
|
|
'FirstName' => 'Sam',
|
|
|
|
'Surname' => 'Minnée',
|
|
|
|
));
|
|
|
|
|
|
|
|
And removing Sig and Sam with that are either age 17 or 74.
|
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->exclude(array(
|
2011-12-08 22:09:48 +01:00
|
|
|
'FirstName' => array('Sam', 'Sig'),
|
|
|
|
'Age' => array(17, 43)
|
|
|
|
));
|
|
|
|
|
|
|
|
This would be equivalent to a SQL query of
|
|
|
|
|
|
|
|
:::
|
|
|
|
... WHERE ("FirstName" NOT IN ('Sam','Sig) OR "Age" NOT IN ('17', '74));
|
|
|
|
|
2012-08-03 07:00:43 +02:00
|
|
|
### Search Filter Modifiers
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
The where clauses showcased in the previous two sections (filter and exclude)
|
|
|
|
specify exact matches by default. However, there are a number of suffixes that
|
|
|
|
you can put on field names to change this behavior such as `":StartsWith"`,
|
2013-10-30 00:03:03 +01:00
|
|
|
`":EndsWith"`, `":PartialMatch"`, `":GreaterThan"`, `":GreaterThanOrEqual"`, `":LessThan"`, `":LessThanOrEqual"`,
|
2013-06-08 05:14:53 +02:00
|
|
|
`":Negation"`.
|
2012-08-03 07:00:43 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Each of these suffixes is represented in the ORM as a subclass of
|
|
|
|
`[api:SearchFilter]`. Developers can define their own SearchFilters if needing
|
|
|
|
to extend the ORM filter and exclude behaviors.
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
These suffixes can also take modifiers themselves. The modifiers currently
|
|
|
|
supported are `":not"`, `":nocase"` and `":case"`. These negate the filter,
|
|
|
|
make it case-insensitive and make it case-sensitive respectively. The default
|
|
|
|
comparison uses the database's default. For MySQL and MSSQL, this is
|
|
|
|
case-insensitive. For PostgreSQL, this is case-sensitive.
|
2012-09-20 09:26:05 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
The following is a query which will return everyone whose first name doesn't
|
|
|
|
start with S, who has logged in since 1/1/2011.
|
2011-04-04 02:18:50 +02:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->filter(array(
|
2014-01-31 02:55:53 +01:00
|
|
|
'FirstName:StartsWith:not' => 'S'
|
2011-04-04 02:18:50 +02:00
|
|
|
'LastVisited:GreaterThan' => '2011-01-01'
|
|
|
|
));
|
|
|
|
|
2012-01-25 23:53:12 +01:00
|
|
|
### Subtract
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can subtract entries from a DataList by passing in another DataList to
|
|
|
|
`subtract()`
|
2012-01-25 23:53:12 +01:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$allSams = Member::get()->filter('FirstName', 'Sam');
|
|
|
|
$allMembers = Member::get();
|
2012-01-25 23:53:12 +01:00
|
|
|
$noSams = $allMembers->subtract($allSams);
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Though for the above example it would probably be easier to use `filter()` and
|
|
|
|
`exclude()`. A better use case could be when you want to find all the members
|
|
|
|
that does not exist in a Group.
|
2012-01-25 23:53:12 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
// ... Finding all members that does not belong to $group.
|
2012-05-09 01:29:24 +02:00
|
|
|
$otherMembers = Member::get()->subtract($group->Members());
|
2012-01-25 23:53:12 +01:00
|
|
|
|
2012-11-27 08:59:31 +01:00
|
|
|
### Limit
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can limit the amount of records returned in a DataList by using the
|
|
|
|
`limit()` method.
|
2012-11-27 08:59:31 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
// Returning the first 5 members, sorted alphabetically by Surname
|
|
|
|
$members = Member::get()->sort('Surname')->limit(5);
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
`limit()` accepts two arguments, the first being the amount of results you want
|
|
|
|
returned, with an optional second parameter to specify the offset, which allows
|
|
|
|
you to tell the system where to start getting the results from. The offset, if
|
|
|
|
not provided as an argument, will default to 0.
|
2012-11-27 08:59:31 +01:00
|
|
|
|
|
|
|
:::php
|
2013-01-15 10:13:46 +01:00
|
|
|
// Return 10 members with an offset of 4 (starting from the 5th result).
|
|
|
|
// Note that the argument order is different from a MySQL LIMIT clause
|
|
|
|
$members = Member::get()->sort('Surname')->limit(10, 4);
|
2012-11-27 08:59:31 +01:00
|
|
|
|
2011-04-04 02:18:50 +02:00
|
|
|
### Raw SQL options for advanced users
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Occasionally, the system described above won't let you do exactly what you need
|
|
|
|
to do. In these situations, we have methods that manipulate the SQL query at a
|
|
|
|
lower level. When using these, please ensure that all table & field names are
|
|
|
|
escaped with double quotes, otherwise some DB back-ends (e.g. PostgreSQL) won't
|
|
|
|
work.
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Under the hood, query generation is handled by the `[api:DataQuery]` class. This
|
|
|
|
class does provide more direct access to certain SQL features that `DataList`
|
|
|
|
abstracts away from you.
|
2012-09-20 13:28:54 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
In general, we advise against using these methods unless it's absolutely
|
|
|
|
necessary. If the ORM doesn't do quite what you need it to, you may also
|
|
|
|
consider extending the ORM with new data types or filter modifiers (that
|
|
|
|
documentation still needs to be written)
|
2011-04-04 02:18:50 +02:00
|
|
|
|
|
|
|
#### Where clauses
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can specify a WHERE clause fragment (that will be combined with other
|
|
|
|
filters using AND) with the `where()` method:
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2012-08-03 07:00:43 +02:00
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->where("\"FirstName\" = 'Sam'")
|
2011-04-04 02:18:50 +02:00
|
|
|
|
|
|
|
#### Joining
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can specify a join with the innerJoin and leftJoin methods. Both of these
|
|
|
|
methods have the same arguments:
|
2011-04-04 02:18:50 +02:00
|
|
|
|
|
|
|
* The name of the table to join to
|
|
|
|
* The filter clause for the join
|
|
|
|
* An optional alias
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2012-08-03 07:00:43 +02:00
|
|
|
:::php
|
2011-04-04 02:18:50 +02:00
|
|
|
// Without an alias
|
2013-06-08 05:14:53 +02:00
|
|
|
$members = Member::get()
|
|
|
|
->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
|
|
|
|
|
|
|
$members = Member::get()
|
|
|
|
->innerJoin("Group_Members", "\"Rel\".\"MemberID\" = \"Member\".\"ID\"", "Rel");
|
2011-04-04 02:18:50 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Passing a *$join* statement to DataObject::get will filter results further by
|
|
|
|
the JOINs performed against the foreign table. **It will NOT return the
|
|
|
|
additionally joined data.** The returned *$records* will always be a
|
2011-04-04 02:18:50 +02:00
|
|
|
`[api:DataObject]`.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
## Properties
|
|
|
|
|
|
|
|
|
|
|
|
### Definition
|
|
|
|
|
|
|
|
Data is defined in the static variable $db on each class, in the format:
|
|
|
|
`<property-name>` => "data-type"
|
|
|
|
|
|
|
|
:::php
|
|
|
|
class Player extends DataObject {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"FirstName" => "Varchar",
|
|
|
|
"Surname" => "Varchar",
|
|
|
|
"Description" => "Text",
|
2013-10-04 13:53:08 +02:00
|
|
|
"Status" => "Enum(array('Active', 'Injured', 'Retired'))",
|
2011-02-07 07:48:44 +01:00
|
|
|
"Birthday" => "Date"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
See [data-types](data-types) for all available types.
|
|
|
|
|
|
|
|
### Overloading
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
"Getters" and "Setters" are functions that help us save fields to our data
|
|
|
|
objects. By default, the methods getField() and setField() are used to set data
|
|
|
|
object fields. They save to the protected array, $obj->record. We can overload
|
|
|
|
the default behavior by making a function called "get`<fieldname>`" or
|
|
|
|
"set`<fieldname>`".
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Player extends DataObject {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2013-10-04 13:53:08 +02:00
|
|
|
"Status" => "Enum(array('Active', 'Injured', 'Retired'))"
|
2011-02-07 07:48:44 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// access through $myPlayer->Status
|
2012-01-30 23:13:42 +01:00
|
|
|
public function getStatus() {
|
2011-02-07 07:48:44 +01:00
|
|
|
// check if the Player is actually... born already!
|
|
|
|
return (!$this->obj("Birthday")->InPast()) ? "Unborn" : $this->Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### Customizing
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
We can create new "virtual properties" which are not actually listed in
|
|
|
|
`private static $db` or stored in the database-row.
|
|
|
|
|
|
|
|
Here we combined a Player's first name and surname, accessible through
|
|
|
|
$myPlayer->Title.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2012-01-30 23:13:42 +01:00
|
|
|
public function getTitle() {
|
2011-02-07 07:48:44 +01:00
|
|
|
return "{$this->FirstName} {$this->Surname}";
|
|
|
|
}
|
|
|
|
|
|
|
|
// access through $myPlayer->Title = "John Doe";
|
2013-06-08 05:14:53 +02:00
|
|
|
// just saves data on the object, please use $myPlayer->write() to save
|
|
|
|
// the database-row
|
2012-01-30 23:13:42 +01:00
|
|
|
public function setTitle($title) {
|
2011-02-07 07:48:44 +01:00
|
|
|
list($firstName, $surName) = explode(' ', $title);
|
|
|
|
$this->FirstName = $firstName;
|
|
|
|
$this->Surname = $surName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
<div class="warning" markdown='1'>
|
2013-06-08 05:14:53 +02:00
|
|
|
**CAUTION:** It is common practice to make sure that pairs of custom
|
|
|
|
getters/setter deal with the same data, in a consistent format.
|
2011-03-08 22:05:51 +01:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
<div class="warning" markdown='1'>
|
2013-06-08 05:14:53 +02:00
|
|
|
**CAUTION:** Custom setters can be hard to debug: Please double check if you
|
|
|
|
could transform your data in more straight-forward logic embedded to your custom
|
|
|
|
controller or form-saving.
|
2011-03-08 22:05:51 +01:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Default Values
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Define the default values for all the $db fields. This example sets the
|
|
|
|
"Status"-column on Player to "Active" whenever a new object is created.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $defaults = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Status" => 'Active',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
<div class="notice" markdown='1'>
|
2013-06-08 05:14:53 +02:00
|
|
|
Note: Alternatively you can set defaults directly in the database-schema (rather
|
|
|
|
than the object-model). See [data-types](data-types) for details.
|
2011-03-08 22:05:51 +01:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Casting
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Properties defined in *static $db* are automatically casted to their
|
|
|
|
[data-types](data-types) when used in templates.
|
|
|
|
|
|
|
|
You can also cast the return-values of your custom functions (e.g. your "virtual
|
|
|
|
properties"). Calling those functions directly will still return whatever type
|
|
|
|
your PHP code generates, but using the *obj()*-method or accessing through a
|
|
|
|
template will cast the value according to the $casting-definition.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $casting = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"MembershipFee" => 'Currency',
|
|
|
|
);
|
|
|
|
|
|
|
|
// $myPlayer->MembershipFee() returns a float (e.g. 123.45)
|
|
|
|
// $myPlayer->obj('MembershipFee') returns a object of type Currency
|
2013-06-08 05:14:53 +02:00
|
|
|
// In a template: <% loop $MyPlayer %>MembershipFee.Nice<% end_loop %>
|
|
|
|
// returns a casted string (e.g. "$123.45")
|
2012-01-30 23:13:42 +01:00
|
|
|
public function getMembershipFee() {
|
2011-02-07 07:48:44 +01:00
|
|
|
return $this->Team()->BaseFee * $this->MembershipYears;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
## Relations
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Relations are built through static array definitions on a class, in the format
|
|
|
|
`<relationship-name> => <classname>`.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### has_one
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
A 1-to-1 relation creates a database-column called "`<relationship-name>`ID", in
|
|
|
|
the example below this would be "TeamID" on the "Player"-table.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
// access with $myPlayer->Team()
|
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $has_one = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Team" => "Team",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
SilverStripe's `[api:SiteTree]` base-class for content-pages uses a 1-to-1
|
|
|
|
relationship to link to its parent element in the tree:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
// access with $mySiteTree->Parent()
|
|
|
|
class SiteTree extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
private static $has_one = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Parent" => "SiteTree",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
### has_many
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Defines 1-to-many joins. A database-column named ""`<relationship-name>`ID""
|
|
|
|
will to be created in the child-class.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
<div class="warning" markdown='1'>
|
2013-06-08 05:14:53 +02:00
|
|
|
**CAUTION:** Please specify a $has_one-relationship on the related child-class
|
|
|
|
as well, in order to have the necessary accessors available on both ends.
|
2011-03-08 22:05:51 +01:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
// access with $myTeam->Players() or $player->Team()
|
|
|
|
class Team extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $has_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Players" => "Player",
|
|
|
|
);
|
|
|
|
}
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $has_one = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Team" => "Team",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
To specify multiple $has_manys to the same object you can use dot notation to
|
|
|
|
distinguish them like below
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2012-08-03 07:00:43 +02:00
|
|
|
class Person extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $has_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Managing" => "Company.Manager",
|
|
|
|
"Cleaning" => "Company.Cleaner",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-08-03 07:00:43 +02:00
|
|
|
class Company extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $has_one = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Manager" => "Person",
|
|
|
|
"Cleaner" => "Person"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Multiple $has_one relationships are okay if they aren't linking to the same
|
|
|
|
object type.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
/**
|
|
|
|
* THIS IS BAD
|
|
|
|
*/
|
|
|
|
class Team extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $has_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Players" => "Player",
|
|
|
|
);
|
|
|
|
}
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
private static $has_one = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Team" => "Team",
|
|
|
|
"AnotherTeam" => "Team",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-30 00:47:11 +02:00
|
|
|
### belongs_to
|
|
|
|
|
|
|
|
Defines a 1-to-1 relationship with another object, which declares the other end
|
|
|
|
of the relationship with a corresponding $has_one. A single database column named
|
|
|
|
`<relationship-name>ID` will be created in the object with the $has_one, but
|
|
|
|
the $belongs_to by itself will not create a database field. This field will hold
|
|
|
|
the ID of the object declaring the $belongs_to.
|
|
|
|
|
|
|
|
Similarly with $has_many, dot notation can be used to explicitly specify the $has_one
|
|
|
|
which refers to this relation. This is not mandatory unless the relationship would
|
|
|
|
be otherwise ambiguous.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
|
|
|
|
class Torso extends DataObject {
|
|
|
|
// HeadID will be generated on the Torso table
|
|
|
|
private static $has_one = array(
|
|
|
|
'Head' => 'Head'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Head extends DataObject {
|
|
|
|
// No database field created. The '.Head' suffix could be omitted
|
|
|
|
private static $belongs_to = array(
|
|
|
|
'Torso' => 'Torso.Head'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
### many_many
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Defines many-to-many joins. A new table, (this-class)_(relationship-name), will
|
|
|
|
be created with a pair of ID fields.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
<div class="warning" markdown='1'>
|
2013-06-08 05:14:53 +02:00
|
|
|
**CAUTION:** Please specify a $belongs_many_many-relationship on the related
|
|
|
|
class as well, in order to have the necessary accessors available on both ends.
|
2011-03-08 22:05:51 +01:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
// access with $myTeam->Categories() or $myCategory->Teams()
|
|
|
|
class Team extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $many_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Categories" => "Category",
|
|
|
|
);
|
|
|
|
}
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
class Category extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $belongs_many_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Teams" => "Team",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### Adding relations
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Adding new items to a relations works the same, regardless if you're editing a
|
|
|
|
*has_many*- or a *many_many*. They are encapsulated by `[api:HasManyList]` and
|
|
|
|
`[api:ManyManyList]`, both of which provide very similar APIs, e.g. an `add()`
|
|
|
|
and `remove()` method.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Team extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
// see "many_many"-description for a sample definition of class "Category"
|
2013-06-08 05:14:53 +02:00
|
|
|
private static $many_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Categories" => "Category",
|
|
|
|
);
|
|
|
|
|
2012-06-28 14:51:04 +02:00
|
|
|
public function addCategories(SS_List $cats) {
|
|
|
|
foreach($cats as $cat) $this->Categories()->add($cat);
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-04 02:18:50 +02:00
|
|
|
### Custom Relations
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can use the flexible datamodel to get a filtered result-list without writing
|
|
|
|
any SQL. For example, this snippet gets you the "Players"-relation on a team,
|
|
|
|
but only containing active players.
|
2012-08-03 07:00:43 +02:00
|
|
|
|
2012-06-28 14:51:04 +02:00
|
|
|
See `[api:DataObject::$has_many]` for more info on the described relations.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Team extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
private static $has_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Players" => "Player"
|
|
|
|
);
|
|
|
|
|
2011-04-04 02:18:50 +02:00
|
|
|
// can be accessed by $myTeam->ActivePlayers()
|
2012-01-30 23:13:42 +01:00
|
|
|
public function ActivePlayers() {
|
2012-11-03 04:16:51 +01:00
|
|
|
return $this->Players()->filter('Status', 'Active');
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Note: Adding new records to a filtered `RelationList` like in the example above
|
|
|
|
doesn't automatically set the filtered criteria on the added record.
|
2012-06-28 14:51:04 +02:00
|
|
|
|
2012-11-03 04:16:51 +01:00
|
|
|
### Relations on Unsaved Objects
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can also set *has_many* and *many_many* relations before the `DataObject` is
|
|
|
|
saved. This behaviour uses the `[api:UnsavedRelationList]` and converts it into
|
|
|
|
the correct `RelationList` when saving the `DataObject` for the first time.
|
2012-11-03 04:16:51 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
This unsaved lists will also recursively save any unsaved objects that they
|
|
|
|
contain.
|
2012-11-03 04:16:51 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
As these lists are not backed by the database, most of the filtering methods on
|
|
|
|
`DataList` cannot be used on a list of this type. As such, an
|
|
|
|
`UnsavedRelationList` should only be used for setting a relation before saving
|
|
|
|
an object, not for displaying the objects contained in the relation.
|
2012-11-03 04:16:51 +01:00
|
|
|
|
2012-06-28 11:43:30 +02:00
|
|
|
## Validation and Constraints
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Traditionally, validation in SilverStripe has been mostly handled on the
|
2014-02-19 00:10:02 +01:00
|
|
|
controller through [form validation](/topics/forms#form-validation).
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2012-06-28 11:43:30 +02:00
|
|
|
While this is a useful approach, it can lead to data inconsistencies if the
|
|
|
|
record is modified outside of the controller and form context.
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
Most validation constraints are actually data constraints which belong on the
|
|
|
|
model. SilverStripe provides the `[api:DataObject->validate()]` method for this
|
|
|
|
purpose.
|
2012-06-28 11:43:30 +02:00
|
|
|
|
|
|
|
By default, there is no validation - objects are always valid!
|
|
|
|
However, you can overload this method in your
|
|
|
|
DataObject sub-classes to specify custom validation,
|
|
|
|
or use the hook through `[api:DataExtension]`.
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Invalid objects won't be able to be written - a [api:ValidationException]` will
|
|
|
|
be thrown and no write will occur.
|
|
|
|
|
|
|
|
It is expected that you call validate() in your own application to test that an
|
|
|
|
object is valid before attempting a write, and respond appropriately if it isn't.
|
2012-06-28 11:43:30 +02:00
|
|
|
|
|
|
|
The return value of `validate()` is a `[api:ValidationResult]` object.
|
|
|
|
You can append your own errors in there.
|
|
|
|
|
|
|
|
Example: Validate postcodes based on the selected country
|
|
|
|
|
|
|
|
:::php
|
|
|
|
class MyObject extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-06-28 11:43:30 +02:00
|
|
|
'Country' => 'Varchar',
|
|
|
|
'Postcode' => 'Varchar'
|
|
|
|
);
|
2012-08-03 07:00:43 +02:00
|
|
|
|
2012-06-28 11:43:30 +02:00
|
|
|
public function validate() {
|
|
|
|
$result = parent::validate();
|
|
|
|
if($this->Country == 'DE' && $this->Postcode && strlen($this->Postcode) != 5) {
|
|
|
|
$result->error('Need five digits for German postcodes');
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-29 03:04:17 +02:00
|
|
|
## Maps
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
A map is an array where the array indexes contain data as well as the values.
|
|
|
|
You can build a map from any DataList like this:
|
2011-10-29 03:04:17 +02:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->map('ID', 'FirstName');
|
2011-10-29 03:04:17 +02:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
This will return a map where the keys are Member IDs, and the values are the
|
|
|
|
corresponding FirstName values. Like everything else in the ORM, these maps are
|
|
|
|
lazy loaded, so the following code will only query a single record from the
|
|
|
|
database:
|
2011-10-29 03:04:17 +02:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get()->map('ID', 'FirstName');
|
2011-10-29 03:04:17 +02:00
|
|
|
echo $member[5];
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
This functionality is provided by the `SS_Map` class, which can be used to build
|
|
|
|
a map around any `SS_List`.
|
2011-10-29 03:04:17 +02:00
|
|
|
|
|
|
|
:::php
|
2012-05-09 01:29:24 +02:00
|
|
|
$members = Member::get();
|
2011-10-29 03:04:17 +02:00
|
|
|
$map = new SS_Map($members, 'ID', 'FirstName');
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
Note: You can also retrieve a single property from all contained records
|
|
|
|
through `[api:SS_List->column()]`.
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
## Data Handling
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
When saving data through the object model, you don't have to manually escape
|
|
|
|
strings to create SQL-safe commands. You have to make sure though that certain
|
|
|
|
properties are not overwritten, e.g. *ID* or *ClassName*.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Creation
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myPlayer = new Player();
|
|
|
|
$myPlayer->Firstname = "John"; // sets property on object
|
|
|
|
$myPlayer->write(); // writes row to database
|
|
|
|
|
|
|
|
|
|
|
|
### Update
|
|
|
|
|
|
|
|
:::php
|
2012-08-03 07:00:43 +02:00
|
|
|
$myPlayer = Player::get()->byID(99);
|
2011-02-07 07:48:44 +01:00
|
|
|
if($myPlayer) {
|
|
|
|
$myPlayer->Firstname = "John"; // sets property on object
|
|
|
|
$myPlayer->write(); // writes row to database
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### Batch Update
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myPlayer->update(
|
|
|
|
ArrayLib::filter_keys(
|
|
|
|
$_REQUEST,
|
|
|
|
array('Birthday', 'Firstname')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Alternatively you can use *castedUpdate()* to respect the
|
|
|
|
[data-types](/topics/data-types). This is preferred to manually casting data
|
|
|
|
before saving.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
$myPlayer->castedUpdate(
|
|
|
|
ArrayLib::filter_keys(
|
|
|
|
$_REQUEST,
|
|
|
|
array('Birthday', 'Firstname')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
### onBeforeWrite
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can customize saving-behaviour for each DataObject, e.g. for adding workflow
|
|
|
|
or data customization. The function is triggered when calling *write()* to save
|
|
|
|
the object to the database. This includes saving a page in the CMS or altering a
|
|
|
|
ModelAdmin record.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Example: Disallow creation of new players if the currently logged-in player is
|
|
|
|
not a team-manager.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
|
|
|
|
private static $has_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Teams"=>"Team"
|
|
|
|
);
|
|
|
|
|
2012-01-30 23:13:42 +01:00
|
|
|
public function onBeforeWrite() {
|
2013-06-08 05:14:53 +02:00
|
|
|
// check on first write action, aka "database row creation"
|
|
|
|
// (ID-property is not set)
|
2011-02-07 07:48:44 +01:00
|
|
|
if(!$this->ID) {
|
|
|
|
$currentPlayer = Member::currentUser();
|
|
|
|
if(!$currentPlayer->IsTeamManager()) {
|
|
|
|
user_error('Player-creation not allowed', E_USER_ERROR);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check on every write action
|
|
|
|
if(!$this->record['TeamID']) {
|
2013-06-08 05:14:53 +02:00
|
|
|
user_error('Cannot save player without a valid team', E_USER_ERROR);
|
2011-02-07 07:48:44 +01:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
// CAUTION: You are required to call the parent-function, otherwise
|
|
|
|
// SilverStripe will not execute the request.
|
2011-02-07 07:48:44 +01:00
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
<div class="notice" markdown='1'>
|
2013-06-08 05:14:53 +02:00
|
|
|
Note: There are no separate methods for *onBeforeCreate* and *onBeforeUpdate*.
|
|
|
|
Please check for the existence of $this->ID to toggle these two modes, as shown
|
|
|
|
in the example above.
|
2011-03-08 22:05:51 +01:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### onBeforeDelete
|
|
|
|
|
|
|
|
Triggered before executing *delete()* on an existing object.
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
Example: Checking for a specific [permission](/reference/permission) to delete
|
|
|
|
this type of object. It checks if a member is logged in who belongs to a group
|
|
|
|
containing the permission "PLAYER_DELETE".
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Player extends DataObject {
|
2013-06-08 05:14:53 +02:00
|
|
|
private static $has_many = array(
|
2011-02-07 07:48:44 +01:00
|
|
|
"Teams"=>"Team"
|
|
|
|
);
|
|
|
|
|
2012-01-30 23:13:42 +01:00
|
|
|
public function onBeforeDelete() {
|
2011-02-07 07:48:44 +01:00
|
|
|
if(!Permission::check('PLAYER_DELETE')) {
|
|
|
|
Security::permissionFailure($this);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::onBeforeDelete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
### Saving data with forms
|
|
|
|
|
|
|
|
See [forms](/topics/forms).
|
|
|
|
|
|
|
|
### Saving data with custom SQL
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
See the ["sql queries" topic](/reference/sqlquery) for custom *INSERT*,
|
|
|
|
*UPDATE*, *DELETE* queries.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
## Extending DataObjects
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can add properties and methods to existing `[api:DataObjects]`s like
|
|
|
|
`[api:Member]` (a core class) without hacking core code or subclassing. See
|
|
|
|
`[api:DataExtension]` for a general description, and `[api:Hierarchy]` for the
|
|
|
|
most popular examples.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
## FAQ
|
|
|
|
|
2012-08-03 07:00:43 +02:00
|
|
|
### What's the difference between DataObject::get() and a relation-getter?
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
You can work with both in pretty much the same way, but relationship-getters
|
|
|
|
return a special type of collection:
|
|
|
|
|
|
|
|
A `[api:HasManyList]` or a `[api:ManyManyList]` with relation-specific
|
|
|
|
functionality.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2012-06-28 14:51:04 +02:00
|
|
|
:::php
|
|
|
|
$myTeams = $myPlayer->Team(); // returns HasManyList
|
2012-08-03 07:00:43 +02:00
|
|
|
$myTeam->add($myOtherPlayer);
|