mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
[doc] Update to Silverstripe 4 convention
- The Silverstripe 4 folder structure has been changed from **app/code/** to **app/src/** - Renamed Silverstripe in text. I assume `api:` and `Namespace`, should remain SilverStripe - Added some missing semicolons
This commit is contained in:
parent
a3df66860f
commit
bba872e02c
@ -6,21 +6,21 @@ icon: database
|
|||||||
|
|
||||||
# Introduction to the Data Model and ORM
|
# Introduction to the Data Model and ORM
|
||||||
|
|
||||||
SilverStripe uses an [object-relational model](http://en.wikipedia.org/wiki/Object-relational_model) to represent its
|
Silverstripe uses an [object-relational model](http://en.wikipedia.org/wiki/Object-relational_model) to represent its
|
||||||
information.
|
information.
|
||||||
|
|
||||||
* Each database table maps to a PHP class.
|
* Each database table maps to a PHP class.
|
||||||
* Each database row maps to a PHP object.
|
* Each database row maps to a PHP object.
|
||||||
* Each database column maps to a property on a PHP object.
|
* Each database column maps to a property on a PHP object.
|
||||||
|
|
||||||
All data tables in SilverStripe are defined as subclasses of [DataObject](api:SilverStripe\ORM\DataObject). The [DataObject](api:SilverStripe\ORM\DataObject) class represents a
|
All data tables in Silverstripe are defined as subclasses of [DataObject](api:SilverStripe\ORM\DataObject). The [DataObject](api:SilverStripe\ORM\DataObject) class represents a
|
||||||
single row in a database table, following the ["Active Record"](http://en.wikipedia.org/wiki/Active_record_pattern)
|
single row in a database table, following the ["Active Record"](http://en.wikipedia.org/wiki/Active_record_pattern)
|
||||||
design pattern. Database Columns are defined as [Data Types](/developer_guides/model/data_types_and_casting) in the static `$db` variable
|
design pattern. Database Columns are defined as [Data Types](/developer_guides/model/data_types_and_casting) in the static `$db` variable
|
||||||
along with any [relationships](relations) defined as `$has_one`, `$has_many`, `$many_many` properties on the class.
|
along with any [relationships](relations) defined as `$has_one`, `$has_many`, `$many_many` properties on the class.
|
||||||
|
|
||||||
Let's look at a simple example:
|
Let's look at a simple example:
|
||||||
|
|
||||||
**app/code/Player.php**
|
**app/src/Player.php**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
@ -41,7 +41,7 @@ so on. After writing this class, we need to regenerate the database schema.
|
|||||||
|
|
||||||
## Generating the Database Schema
|
## Generating the Database Schema
|
||||||
|
|
||||||
After adding, modifying or removing `DataObject` subclasses, make sure to rebuild your SilverStripe database. The
|
After adding, modifying or removing `DataObject` subclasses, make sure to rebuild your Silverstripe database. The
|
||||||
database schema is generated automatically by visiting the URL http://www.yoursite.com/dev/build while authenticated as an administrator.
|
database schema is generated automatically by visiting the URL http://www.yoursite.com/dev/build while authenticated as an administrator.
|
||||||
|
|
||||||
This script will analyze the existing schema, compare it to what's required by your data classes, and alter the schema
|
This script will analyze the existing schema, compare it to what's required by your data classes, and alter the schema
|
||||||
@ -60,7 +60,7 @@ It **won't** do any of the following
|
|||||||
* Delete tables
|
* Delete tables
|
||||||
* Delete fields
|
* Delete fields
|
||||||
* Rename any tables that it doesn't recognize. This allows other applications to coexist in the same database, as long as
|
* Rename any tables that it doesn't recognize. This allows other applications to coexist in the same database, as long as
|
||||||
their table names don't match a SilverStripe data class.
|
their table names don't match a Silverstripe data class.
|
||||||
|
|
||||||
|
|
||||||
[notice]
|
[notice]
|
||||||
@ -76,7 +76,7 @@ automatically set on the `DataObject`.
|
|||||||
* Created: A date/time field set to the creation date of this record
|
* Created: A date/time field set to the creation date of this record
|
||||||
* LastEdited: A date/time field set to the date this record was last edited through `write()`
|
* LastEdited: A date/time field set to the date this record was last edited through `write()`
|
||||||
|
|
||||||
**app/code/Player.php**
|
**app/src/Player.php**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
@ -129,7 +129,7 @@ Using the `create()` method provides chainability, which can add elegance and br
|
|||||||
[/notice]
|
[/notice]
|
||||||
|
|
||||||
|
|
||||||
Database columns and properties can be set as class properties on the object. The SilverStripe ORM handles the saving
|
Database columns and properties can be set as class properties on the object. The Silverstripe ORM handles the saving
|
||||||
of the values through a custom `__set()` method.
|
of the values through a custom `__set()` method.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@ -299,7 +299,7 @@ You can also sort randomly. Using the `DB` class, you can get the random sort me
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
$random = DB::get_conn()->random();
|
$random = DB::get_conn()->random();
|
||||||
$players = Player::get()->sort($random)
|
$players = Player::get()->sort($random);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Filtering Results
|
## Filtering Results
|
||||||
@ -642,7 +642,7 @@ you need it to, you may also consider extending the ORM with new data types or f
|
|||||||
You can specify a WHERE clause fragment (that will be combined with other filters using AND) with the `where()` method:
|
You can specify a WHERE clause fragment (that will be combined with other filters using AND) with the `where()` method:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$members = Member::get()->where("\"FirstName\" = 'Sam'")
|
$members = Member::get()->where("\"FirstName\" = 'Sam'");
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Joining Tables
|
#### Joining Tables
|
||||||
@ -692,7 +692,7 @@ Note: Alternatively you can set defaults directly in the database-schema (rather
|
|||||||
## Subclasses
|
## Subclasses
|
||||||
|
|
||||||
Inheritance is supported in the data model: separate tables will be linked together, the data spread across these
|
Inheritance is supported in the data model: separate tables will be linked together, the data spread across these
|
||||||
tables. The mapping and saving logic is handled by SilverStripe, you don't need to worry about writing SQL most of the
|
tables. The mapping and saving logic is handled by Silverstripe, you don't need to worry about writing SQL most of the
|
||||||
time.
|
time.
|
||||||
|
|
||||||
For example, suppose we have the following set of classes:
|
For example, suppose we have the following set of classes:
|
||||||
@ -751,7 +751,7 @@ example above, NewsSection didn't have its own data, so an extra table would be
|
|||||||
* In all the tables, ID is the primary key. A matching ID number is used for all parts of a particular record:
|
* In all the tables, ID is the primary key. A matching ID number is used for all parts of a particular record:
|
||||||
record #2 in Page refers to the same object as record #2 in [SiteTree](api:SilverStripe\CMS\Model\SiteTree).
|
record #2 in Page refers to the same object as record #2 in [SiteTree](api:SilverStripe\CMS\Model\SiteTree).
|
||||||
|
|
||||||
To retrieve a news article, SilverStripe joins the [SiteTree](api:SilverStripe\CMS\Model\SiteTree), [Page](api:SilverStripe\CMS\Model\SiteTree\Page) and NewsPage tables by their ID fields.
|
To retrieve a news article, Silverstripe joins the [SiteTree](api:SilverStripe\CMS\Model\SiteTree), [Page](api:SilverStripe\CMS\Model\SiteTree\Page) and NewsPage tables by their ID fields.
|
||||||
|
|
||||||
## Related Lessons
|
## Related Lessons
|
||||||
* [Introduction to the ORM](https://www.silverstripe.org/learn/lessons/v4/introduction-to-the-orm-1)
|
* [Introduction to the ORM](https://www.silverstripe.org/learn/lessons/v4/introduction-to-the-orm-1)
|
||||||
|
Loading…
Reference in New Issue
Block a user