Beispiel #1
0
function hydrate_model($file)
{
    if (is_file($file) && strpos($file, '.php')) {
        preg_match_all('/class\\s(\\S+)\\s/', read($file), $match);
        require $file;
        foreach ($match[1] as $klass) {
            $re = new \ReflectionClass($klass);
            switch ($re->getParentClass()->getName()) {
                case 'Servant\\Mapper\\Database':
                    status('hydrate', $file);
                    $dsn = option('database.' . $klass::CONNECTION);
                    $db = \Grocery\Base::connect($dsn);
                    $columns = $klass::columns();
                    $indexes = $klass::indexes();
                    if (!isset($db[$klass::table()])) {
                        $db[$klass::table()] = $columns;
                    }
                    \Grocery\Helpers::hydrate($db[$klass::table()], $columns, $indexes);
                    break;
                case 'Servant\\Mapper\\MongoDB':
                    status('hydrate', $file);
                    $dsn_string = \Servant\Config::get($klass::CONNECTION);
                    $database = substr($dsn_string, strrpos($dsn_string, '/') + 1);
                    $mongo = $dsn_string ? new \Mongo($dsn_string) : new \Mongo();
                    $db = $mongo->{$database ?: 'default'};
                    \Servant\Helpers::reindex($db->{$klass::table()}, $klass::indexes());
                    break;
                default:
                    break;
            }
        }
    }
}
Beispiel #2
0
                        });
                        it('should add columns on extra fields', function ($a) {
                            \Grocery\Helpers::hydrate($a, array('x' => 'integer', 'y' => 'integer'));
                        });
                        it('should remove columns on missing fields', function ($a) {
                            \Grocery\Helpers::hydrate($a, array('x' => 'integer'));
                        });
                    });
                    describe('indexes', function () {
                        it('should add indexes when they are provided', function ($a) {
                            \Grocery\Helpers::hydrate($a, array('x' => 'integer', 'y' => 'integer'), array('x'));
                            $test = $a->indexes();
                            expect($test['a_x___idx']['unique'])->toBeFalsy();
                        });
                        it('should set indexes as unique when passing true', function ($a) {
                            \Grocery\Helpers::hydrate($a, array('x' => 'integer', 'y' => 'integer'), array('x', 'y' => true));
                            $test = $a->indexes();
                            expect($test['a_y___idx']['unique'])->toBeTruthy();
                        });
                        it('should remove indexes when they are missing', function ($a) {
                            \Grocery\Helpers::hydrate($a, array('x' => 'integer', 'y' => 'integer'), array('y' => false));
                            $test = $a->indexes();
                            expect($test)->toHaveLength(1);
                        });
                    });
                });
            });
        });
    };
    array_map($suitcase, $datasources);
});