Version: current

# Class YamlFixture

Uses the Spyc library to parse a YAML document (see http://yaml.org). YAML is a simple markup languages that uses tabs and colons instead of the more verbose XML tags, and because of this much better for developers creating files by hand.

The contents of the YAML file are broken into three levels:

- Top level: class names - Page and ErrorPage. This is the name of the dataobject class that should be created. The fact that ErrorPage is actually a subclass is irrelevant to the system populating the database. Each identifier you specify delimits a new database record. This means that every record needs to have an identifier, whether you use it or not.
- Third level: fields - each field for the record is listed as a 3rd level entry. In most cases, the fieldŐs raw content is provided. However, if you want to define a relationship, you can do so using "=>"

There are a couple of lines like this:

    Parent: =>Page.about

This will tell the system to set the ParentID database field to the ID of the Page object with the identifier ŇaboutÓ. This can be used on any has-one or many-many relationship. Note that we use the name of the relationship (Parent), and not the name of the database field (ParentID)

On many-many relationships, you should specify a comma separated list of values.

    MyRelation: =>Class.inst1,=>Class.inst2,=>Class.inst3

An crucial thing to note is that the YAML file specifies DataObjects, not database records. The database is populated by instantiating DataObject objects, setting the fields listed, and calling write(). This means that any onBeforeWrite() or default value logic will be executed as part of the test. This forms the basis of our testURLGeneration() test above.

For example, the URLSegment value of Page.staffduplicate is the same as the URLSegment value of Page.staff. When the fixture is set up, the URLSegment value of Page.staffduplicate will actually be my-staff-2.

Finally, be aware that requireDefaultRecords() is not called by the database populator - so you will need to specify standard pages such as 404 and home in your YAML file.

    Page:
       home:
          Title: Home
       about:
          Title: About Us
       staff:
          Title: Staff
          URLSegment: my-staff
          Parent: =>Page.about
       staffduplicate:
          Title: Staff
          URLSegment: my-staff
          Parent: =>Page.about
       products:
          Title: Products
    ErrorPage:
       404:
         Title: Page not Found
         ErrorCode: 404

  Object
  YamlFixture

Package: sapphire\core
See: http://spyc.sourceforge.net/
Param: $fixtureFile The location of the .yml fixture file, relative to the site base dir
Located at sapphire/core/model/YamlFixture.php

## Methods summary

`public` | # `__construct( mixed $fixtureFile )` #### Overrides `Object::__construct`
`public` | # `idFromFixture( mixed $className, mixed $identifier )` Get the ID of an object from the fixture. Get the ID of an object from the fixture. #### Parameters $className $className The data class, as specified in your fixture file. Parent classes won't work $identifier $identifier The identifier string, as provided in your fixture file
`public A` | # `allFixtureIDs( mixed $className )` Return all of the IDs in the fixture of a particular class name. Return all of the IDs in the fixture of a particular class name. #### Returns `A` map of fixture-identifier => object-id
`public` | # `objFromFixture( mixed $className, mixed $identifier )` Get an object from the fixture. Get an object from the fixture. #### Parameters $className $className The data class, as specified in your fixture file. Parent classes won't work $identifier $identifier The identifier string, as provided in your fixture file
`public` | # `saveIntoDatabase( )` Load a YAML fixture file into the database. Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. Load a YAML fixture file into the database. Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. Caution: In order to support reflexive relations which need a valid object ID, the record is written twice: first after populating all non-relational fields, then again after populating all relations (has_one, has_many, many_many).
`protected` | # `writeDataObject( string $dataClass, array $items )` Writes the fixture into the database using DataObjects Writes the fixture into the database using DataObjects #### Parameters $dataClass string $dataClass $items array $items
`protected` | # `writeSQL( string $table, array $items )` Writes the fixture into the database directly using a database manipulation Writes the fixture into the database directly using a database manipulation #### Parameters $table string $table $items array $items
`protected` | # `parseFixtureVal( mixed $fieldVal )` Parse a value from a fixture file. If it starts with => it will get an ID from the fixture dictionary Parse a value from a fixture file. If it starts with => it will get an ID from the fixture dictionary

### Methods inherited from Object

`__call()`, `__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `create()`, `createMethod()`, `create_from_string()`, `defineMethods()`, `exists()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Magic methods summary

## Properties summary

`protected string` | $fixtureFile | # The location of the .yml fixture file, relative to the site base dir The location of the .yml fixture file, relative to the site base dir
`protected array` | $fixtureDictionary | # Array of fixture items Array of fixture items

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -