Wiki

Clone wiki

ovv-intranet / Fixtures

Fixtures loader

A simple tool to load fixtures.

How to use?

Files with fixtures should be placed in directory app/module/module_name/fixtures. A file with fixture should extends an adapter from Vegas package, for example: \Vegas\Fixtures\Adapter\PhpObject.

Each fixture must define a model in a constructor, also in the constructor we can put another fixture/fixtures as a dependency (fixtures from dependencies will be executed earlier).

An example file with fixture

<?php

namespace News\Fixtures;

use News\Models;

class News extends \Vegas\Fixtures\Adapter\PhpObject implements \Phalcon\DI\InjectionAwareInterface
{
    use \Vegas\DI\InjectionAwareTrait;

    public function __construct() 
    {
        // Optional
        $this->dependencies[] = '\News\Fixtures\Category';

        // Required!
        $this->model = '\News\Models\News';
    }

    // Required!
    public function generate() 
    {
        $category = Models\Category::findFirst();

        for ($i=0; $i<10; $i++) {
            $this->fixtures[$i] = [
                'category_id' => $category->_id,
                'fixed_position' => (string)$i,
                'title' => 'test'.$i,
                'content' => 'test test',
                'visible_from' => new \MongoInt32(1397167200),
                'visible_to' => '',
                'visible' => '1',
                'slug' => 'test'.$i,
                'created_date' => date('Y-m-d'),
                'files' => NULL,
            ];
        }
    } 
}

Updated