https://github.com/linkorb/haigha
Alice fixtures for database tables
https://github.com/linkorb/haigha
Last synced: about 1 year ago
JSON representation
Alice fixtures for database tables
- Host: GitHub
- URL: https://github.com/linkorb/haigha
- Owner: linkorb
- License: mit
- Created: 2015-02-25T14:56:53.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-05-16T11:25:39.000Z (about 2 years ago)
- Last Synced: 2025-03-30T07:04:12.406Z (about 1 year ago)
- Language: PHP
- Size: 44.9 KB
- Stars: 24
- Watchers: 10
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
> [!TIP]
> Haigha has been superseeded by the brand new [Hatter](https://github.com/linkorb/haigha) - similar features, still application independant, but no longer trying to use Alice in a way that it wasn't designed for.
# Haigha: Alice fixtures for tables

[Alice](https://github.com/nelmio/alice) is an *awesome* database fixtures library.
It works with Doctrine out-of-the-box, but if you don't use the Doctrine ORM, you'll need custom persisters...
This is where **Haigha** comes in:
> *Haigha lets you use Alice directly with database tables!*
## Features
* Supports all standard Alice functionality (ranges, optional data, references, inheritence, etc)
* Supports Faker data providers
* Supports any PDO connection
* No need to write classes, directly persist from yml to your sql database
## Example fixture file
Haigha uses Alice to load fixture files, so the format is identical ([Details](https://github.com/nelmio/alice)). The only thing to keep in mind is that you use tablenames instead of classnames. Prefix your tablenames with `table.`. For example, if your tablename is called `user`, you use it like this:
```yaml
table.group:
group_random_users:
id: 1 # This is important for version ~2.0
name: Random users
table.user:
random_user{0..9}:
group_id: @group_random_users
username:
firstname:
lastname:
password:
email:
```
## How to use Haigha in your application
Simply add the following to your `require` or `require-dev` section in your [composer.json](http://getcomposer.org) and run `composer update`:
```json
"require": {
"linkorb/haigha": "^2.0"
}
```
You can now use Haigha in your applications, or use the included command-line tool to load fixtures into your database:
## Command-line usage
The haigha command-line tool knows one sub-command: `fixtures:load`.
The first argument is the filename of your fixture definition (yaml).
The second argument is the (optional) database url. If no database url is specified, haigha used your `PDO` environment variable instead.
### Database URL
A full URL containing username, password, hostname and dbname. For example:
```
./vendor/bin/haigha fixtures:load examples/random_users.yml mysql://username:password@hostname/dbname
```
### Just a dbname
In this case [linkorb/database-manager](https://github.com/linkorb/database-manager) is used for loading database connection details (server, username, password, etc) from .conf files (read project readme for more details).
In a nutshell - you must have a `database_name.conf` file at `/share/config/database/` as described at [database-manager's documentation](https://github.com/linkorb/database-manager#database-configuration-files).
```bash
./vendor/bin/haigha fixtures:load examples/random_users.yml dbname
```
## Library usage:
You can use Haigha in your own application like this:
```php
// Instantiate a new Alice loader
$loader = new Nelmio\Alice\Fixtures\Loader();
// Add the Haigha instantiator
$instantiator = new Haigha\TableRecordInstantiator();
$loader->addInstantiator($instantiator);
// Load (Haigha) objects from a Alice yml file
$objects = $loader->load('examples/random_users.yml');
// Instantiate the Haigha PDO persister, and pass a PDO connection
$persister = new PdoPersister($pdo);
// Persist the Haigha objects on the PDO connection
$persister->persist($objects);
```
## Test
Customize `phpunit.xml`:
```
cp phpunit.xml.dist phpunit.xml
```
Run:
```
vendor/bin/phpunit
```
## License
MIT (see [LICENSE.md](LICENSE.md))
## Brought to you by the LinkORB Engineering team

Check out our other projects at [linkorb.com/engineering](http://www.linkorb.com/engineering).
Btw, we're hiring!