PHPackages                             khepin/yaml-fixtures-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. khepin/yaml-fixtures-bundle

AbandonedArchivedSymfony-bundle[Database &amp; ORM](/categories/database)

khepin/yaml-fixtures-bundle
===========================

Symfony bundle for easy loading of YAML based fixtures.

1.0.2(8y ago)58148.5k40[9 PRs](https://github.com/khepin/KhepinYamlFixturesBundle/pulls)1MITPHP

Since Feb 21Pushed 8y ago5 watchersCompare

[ Source](https://github.com/khepin/KhepinYamlFixturesBundle)[ Packagist](https://packagist.org/packages/khepin/yaml-fixtures-bundle)[ Docs](https://github.com/khepin/KhepinYamlFixturesBundle)[ RSS](/packages/khepin-yaml-fixtures-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (19)Used By (1)

This bundles provides you with a way to use YAML based fixtures for Symfony2 and Doctrine2. It currently works with either Doctrine ORM or Doctrine MongoDB ODM. Other backend are not implemented yet but can be implemented very easily.

**Travic CI status:** [![Build Status](https://camo.githubusercontent.com/393e88663e329334caf2c826c957978abc2b992ad6a41e1bedd66924ae65c7c5/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6b686570696e2f4b686570696e59616d6c466978747572657342756e646c652e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/khepin/KhepinYamlFixturesBundle)**Scrutinizer CI:** [![Scrutinizer Code Quality](https://camo.githubusercontent.com/c924daf6eb5cabfa532b0c6d1976d17a0aa30fb961450c08973de70b05a83d21/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b686570696e2f4b686570696e59616d6c466978747572657342756e646c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/khepin/KhepinYamlFixturesBundle/?branch=master)

Installation
============

[](#installation)

This bundle depends on the [DoctrineFixturesBundle](http://symfony.com/doc/2.0/bundles/DoctrineFixturesBundle/index.html). If you don't have configured it with Symfony2 yet, follow the [setup instructions](http://symfony.com/doc/2.0/bundles/DoctrineFixturesBundle/index.html#setup-and-configuration).

Through [Composer](http://getcomposer.org), add to composer.json:

```
"require": {
    "khepin/yaml-fixtures-bundle": "~1.0.1"
}

```

Then register the bundle in `AppKernel.php` it's better to only register it in the dev environment as it shouldn't be needed elsewhere.

```
public function registerBundles()
{
    if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        //...
        $bundles[] = new Khepin\YamlFixturesBundle\KhepinYamlFixturesBundle();
        //...
    }
}

```

Configuration
=============

[](#configuration)

In your `config.yml` or `config_dev.yml` (recommended) add the following:

```
khepin_yaml_fixtures:
    resources:
        - MyOtherBundle/load_this_first
        - MyBundle
        - MyOtherBundle

```

Under 'resources' is a list of the bundles that have fixtures that you wish to load. The fixtures will be loaded in that order.

The `MyOtherBundle/load_this_first` syntax means that `load_this_first.yml` will be loaded before The rest of the files in this bundle. This allows to set any specific order for loading fixture files.

Define your fixture files
=========================

[](#define-your-fixture-files)

Setup
-----

[](#setup)

It is important to note that unlike in Symfony 1.x, the order in which you load your fixtures *does* matter. There are 2 ways you can manipulate that order:

- Via `config.yml`: specify which bundles have their fixtures loaded first
- Via file name: fixture files are loaded in alphabetical order inside of each bundle

By default, fixture files use the bundle hierarchy: `MyBundle/DataFixtures/somefixtures.yml`.

If you want to change the hierarchy fixtures use, specify it in your configuration:

```
khepin_yaml_fixtures:
    directory: Resources/fixtures

```

This will cause your fixture files to use the bundle hierarchy: `MyBundle/Resources/fixtures/somefixtures.yml`.

Definition
----------

[](#definition)

You can only define fixtures for one class per file. Fixture files are configured at the top level, and defined within the `fixtures` key. You can name a fixture to be referenced later by supplying a name in the `fixtures` array.

```
model: Name\Space\MyBundle\Entity\User
tags: [ test, dev, prod ] # optional parameter
save_in_reverse: false # optional parameter
persistence: orm (default)| mongodb # lets you choose if these fixtures should be saved through the orm or through mongodb.
fixtures:
    michael:
        name: Michael
        phonenumber: 8765658
        birthday: "1989-12-12"

```

You can use references to previously created fixtures by supplying the name:

```
model: Name\Space\MyBundle\Entity\Car
fixtures:
    audi_a3:
        owner: michael
        since: "2010-12-12"

```

For [MongoDB's reference many](http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/reference-mapping.html#reference-many), include your references as a list under the corresponding key:

```
model: Name\Space\Bundle\Document\Car
persistence: mongodb
fixtures:
    audi_a3:
        owners:
            - michael
            - paul
            - angella

```

You can also define as many files as you want for the same entity. This will be useful when used together with context tags (see below).

\##Work with dates and times:

Dates need to be set inside quotes. Dates are passed to DateTime, so any [string that will work will DateTime](http://www.php.net/manual/en/datetime.formats.php) will work here. That includes the relative formats like "-1 day", "next month", "1 week ago".

```
model: Name\Space\MyBundle\Entity\Statistics
fixtures:
    stat-1:
        product: example.org
        date: "2010-12-12" #dates NEED to be set inside quotes

```

Mongo embedded documents
------------------------

[](#mongo-embedded-documents)

It's possible to use embedded documents in mongo (only embed\_one is implemented at this time). Just keep cascading your yaml file like this:

```
model: Name\Space\Bundle\Document\Article
persistence: mongodb
fixtures:
    first_post:
        title: Ouelkom to my niew blog!
        content: I will update regularly!
        author: # This defines an embedded document
            name: khepin # this will be set on the embedded document

```

Usage
=====

[](#usage)

From the command line
---------------------

[](#from-the-command-line)

```
php app/console khepin:yamlfixtures:load

```

More later regarding contexts, there is no need to add a context unless you have a reason to.

**ATTENTION**: the command line can only load one context at a time for now.

From anywhere else
------------------

[](#from-anywhere-else)

If you need to load the fixtures from anywhere else like say ... your functional tests in order to setup a clean database for testing, you can access the same thing through the service container with the added advantage of being able to load multiple contexts together:

```
$container->get('khepin.yaml_loader')->loadFixtures('prod', 'test', ...);

```

About contexts
--------------

[](#about-contexts)

Sometimes when setting up fixtures for testing purpose, you need to have different configurations. This is what the context aims to help solving.

The contexts are equivalent to the tags set in the fixture file under the tag key. You can set as many tags as you want on a fixture file. Such as `prod`, `test` etc...

If you define fixtures in this way, then from the command line, calling:

```
php app/console khepin:yamlfixtures:load prod

```

All the fixture files for which you have set:

```
tags: [ ..., prod, ... ]

```

Will be loaded. This way you can define fixtures that are loaded whenever you use a test or dev environment but are not loaded in prod for example.

A fixture file with no tags at all is **always** loaded! This way you can setup your bootstrapping fixtures in files that have absolutely no tags and then have fixtures specific for each purpose.

And what the heck is this "save\_in\_reverse" thingy?
-----------------------------------------------------

[](#and-what-the-heck-is-this-save_in_reverse-thingy)

This parameter can be omitted most of the time. It's only useful so far when you have a self referencing table. For example if you had fixtures like this:

```
fixtures:
    last_level:
        next_level: none
        name: Meet the boss
    middle_level:
        next_level: last_level
        name: complete the quest
    start_level:
        next_level: middle_level
        name: introduction

```

In this case, we need to put `last_level` first in our fixtures since it's the only one that doesn't reference anything else. We could not create `start_level` first because it needs `middle_level` to already exist etc...

The problem with this is that when purging the database, the ORMPurger() goes through rows one by one ordered by their ids. So if we save them in this order, `last_level`should be the first to go away which will cause a problems with foreign keys as it is still referenced by `middle_level`.

Save in reverse will create the objects in this order so the references are set properly and then save them in the opposite order so there is no exception when purging the database.

Handling ORM One-To-Many or Many-To-Many Associations
-----------------------------------------------------

[](#handling-orm-one-to-many-or-many-to-many-associations)

If you want to pass an array of already created objects to a \*-To-Many assocation, you can do this by first allowing your setter on the object to accept a plain PHP array (as opposed to only accepting a Doctrine\\Common\\ArrayCollection) and then define your YAML file as follows:

```
fixtures:
    car:
        name: foo_bar
        parts:
            - part_one
            - part_two
            - part_three

```

This is assuming of course that part\_one, part\_two, and part\_three are objects you already defined in previously loaded files.

The YAML loader will create a plain PHP array of the three objects and pass it to, for example, setParts() on the model you are defining in this file.

Constructor
-----------

[](#constructor)

If you want to pass arguments to a constructor :

```
fixtures:
    car:
        __construct:
            - Ford
            - {type: reference , value: owner_ford}
            - {type: datetime, value: "2012-01-01"}

```

```
class Car
{
    /**
     * @param string $name
     * @param Owner $owner
     * @param DateTime $purchaseDate
     */
    public function __construct($name, Owner $owner, \DateTime $purchaseDate, )
    {
        ...
    }
}
```

Service calls
-------------

[](#service-calls)

Some entities require being managed by a special service before they can be persisted. This is the case with FOSUserBundle for example where the right password is set by the `user_manager` and not directly in the user class. Therefore we need to ask this service to set our domain object in the correct state before it can be persisted. Service calls are declared this way:

```
model: My\NameSpace\User
service_calls:
    service_1:
        service: fos_user.user_manager # this is the service id in the container
        method: updateUser # the method to be called on the object
fixtures:
  dad:
    name: Francois
    plain_password: thisismypassword

```

Now for each user, before it is persisted, something equivalent to the following code will happen:

```
$container->get('fos_user.user_manager')->updateUser($user_francois);

```

Using ACLs
----------

[](#using-acls)

If you need to set ACL entries on your fixtures, it is possible. The ACLs are created after all fixtures have been saved so that there is no possible conflict.

To set ACLs for the fixtures, you need to be using [ProblematicAclManagerBundle](problematic/ProblematicAclManagerBundle).

And to update your configuratin as follows:

```
khepin_yaml_fixtures:
    acl_manager: ~
    resources:
        - MyBundle
        - MyOtherBundle

```

The ACLs can only use the standard defined masks from the Symfony MaskBuilder. Example:

```
model: My\NameSpace\Car
tags: [ test ]
fixtures:
  dad_car:
    name: Mercedes
  mom_car:
    name: Mini Cooper

acl:
  dad_car:
    user_dad: MASK_OWNER
    user_mom: MASK_MASTER
  mom_car:
    user_mom: MASK_OWNER

```

Be careful that the ACLs in Symfony are not managed through Doctrine and therefore will not be purged when you re-create your fixtures. However if any conflicts, loading the ACLs will overwrite all previous ACL entries.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 86.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~123 days

Recently: every ~404 days

Total

17

Last Release

3213d ago

Major Versions

v0.8.1 → 1.0.02015-01-16

### Community

Maintainers

![](https://www.gravatar.com/avatar/287ba80990994822ffc6808fc979762030bc19ce4095a8fa641b4e7d3dc10627?d=identicon)[Khepin](/maintainers/Khepin)

---

Top Contributors

[![khepin](https://avatars.githubusercontent.com/u/455656?v=4)](https://github.com/khepin "khepin (145 commits)")[![lenardpalko](https://avatars.githubusercontent.com/u/1013821?v=4)](https://github.com/lenardpalko "lenardpalko (4 commits)")[![claudusd](https://avatars.githubusercontent.com/u/3321326?v=4)](https://github.com/claudusd "claudusd (3 commits)")[![buzzedword](https://avatars.githubusercontent.com/u/334485?v=4)](https://github.com/buzzedword "buzzedword (3 commits)")[![youbs](https://avatars.githubusercontent.com/u/346708?v=4)](https://github.com/youbs "youbs (3 commits)")[![dsbaars](https://avatars.githubusercontent.com/u/480514?v=4)](https://github.com/dsbaars "dsbaars (2 commits)")[![unti1x](https://avatars.githubusercontent.com/u/3488839?v=4)](https://github.com/unti1x "unti1x (1 commits)")[![greg0ire](https://avatars.githubusercontent.com/u/657779?v=4)](https://github.com/greg0ire "greg0ire (1 commits)")[![AlexZheleznyakov](https://avatars.githubusercontent.com/u/603182?v=4)](https://github.com/AlexZheleznyakov "AlexZheleznyakov (1 commits)")[![bamarni](https://avatars.githubusercontent.com/u/1205386?v=4)](https://github.com/bamarni "bamarni (1 commits)")[![66Ton99](https://avatars.githubusercontent.com/u/383739?v=4)](https://github.com/66Ton99 "66Ton99 (1 commits)")[![llsousa](https://avatars.githubusercontent.com/u/180369?v=4)](https://github.com/llsousa "llsousa (1 commits)")[![mikeSimonson](https://avatars.githubusercontent.com/u/907613?v=4)](https://github.com/mikeSimonson "mikeSimonson (1 commits)")

---

Tags

data fixtures doctrine yaml

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/khepin-yaml-fixtures-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/khepin-yaml-fixtures-bundle/health.svg)](https://phpackages.com/packages/khepin-yaml-fixtures-bundle)
```

###  Alternatives

[hautelook/alice-bundle

Symfony bundle to manage fixtures with Alice and Faker.

19519.4M34](/packages/hautelook-alice-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[damienharper/auditor-bundle

Integrate auditor library in your Symfony projects.

4542.8M](/packages/damienharper-auditor-bundle)[kimai/kimai

Kimai - Time Tracking

4.6k7.4k1](/packages/kimai-kimai)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[bolt/core

🧿 Bolt Core

585142.5k54](/packages/bolt-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
