1. Go to this page and download the library: Download k-kinzal/sql-fixture library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
k-kinzal / sql-fixture example snippets
use Faker\Factory;
use SqlFixture\FixtureProvider;
$faker = Factory::create();
$faker->addProvider(new FixtureProvider($faker));
$fixture = $faker->fixture(
'CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL,
age INT UNSIGNED,
status ENUM("active", "inactive"),
balance DECIMAL(10, 2),
created_at DATETIME
)'
);
// Returns: ['name' => 'John Doe', 'email' => '[email protected]', 'age' => 28, ...]
// Note: auto_increment columns (id) are skipped by default
use Faker\Factory;
use SqlFixture\DatabaseFixtureProvider;
$pdo = new PDO('mysql:host=localhost;dbname=myapp', 'user', 'password');
$faker = Factory::create();
$faker->addProvider(new DatabaseFixtureProvider($faker, $pdo));
// Generate fixture by table name
$fixture = $faker->fixture('users');
// Schema is fetched via SHOW CREATE TABLE and cached
use Faker\Factory;
use SqlFixture\FileFixtureProvider;
// Directory contains: users.sql, posts.sql, comments.sql
$faker = Factory::create();
$faker->addProvider(new FileFixtureProvider($faker, '/path/to/ddl/'));
$fixture = $faker->fixture('users');
// List available tables
$tables = $faker->getTableNames(); // ['users', 'posts', 'comments']
// Register additional schemas at runtime
$faker->registerSchema('CREATE TABLE tags (id INT PRIMARY KEY, name VARCHAR(50))');
class User
{
public function __construct(
public readonly int $id,
public readonly string $userName,
public readonly string $email,
) {}
}
$user = $faker->fixture(
'CREATE TABLE users (id INT PRIMARY KEY, user_name VARCHAR(255), email VARCHAR(255))',
['id' => 1],
User::class
);
// Returns: User(id: 1, userName: 'generated_name', email: '[email protected]')
use SqlFixture\FixtureProvider;
use SqlFixture\Platform\PlatformFactory;
// Specify dialect explicitly
$faker->addProvider(new FixtureProvider($faker, dialect: PlatformFactory::DRIVER_SQLITE));
$fixture = $faker->fixture(
'CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
score REAL
)'
);
// DatabaseFixtureProvider auto-detects dialect from PDO driver
$pdo = new PDO('sqlite::memory:');
$faker->addProvider(new DatabaseFixtureProvider($faker, $pdo));
// SQLite dialect is automatically detected
use SqlFixture\FixtureProvider;
use SqlFixture\TypeMapper\TypeMapperInterface;
use SqlFixture\Schema\SchemaParserInterface;
use SqlFixture\Hydrator\HydratorInterface;
$provider = new FixtureProvider(
$faker,
typeMapper: $customTypeMapper, // Custom type-to-value mapping
hydrator: $customHydrator, // Custom object hydration
schemaParser: $customSchemaParser, // Custom CREATE TABLE parsing
dialect: 'mysql',
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.