PDOdb provides convenient command-line tools for common development tasks, including migration generation, model generation, schema inspection, and interactive query testing.
The CLI tools are designed to streamline your development workflow:
- Migration Generator - Create database migrations with interactive prompts
- Model Generator - Generate ActiveRecord models from existing database tables
- Schema Inspector - Inspect database schema structure
- Query Tester - Interactive REPL for testing SQL queries
CLI tools automatically detect database configuration from:
.envfile in the current working directory (recommended for production)config.phpfile in the current working directory- Environment variables (
PDODB_DRIVER,PDODB_HOST, etc.) - Examples config (for testing only)
The following environment variables are supported:
# Database driver (mysql, mariadb, pgsql, sqlite, sqlsrv)
PDODB_DRIVER=mysql
# Connection settings
PDODB_HOST=localhost
PDODB_PORT=3306
PDODB_DATABASE=mydb
PDODB_USERNAME=user
PDODB_PASSWORD=password
PDODB_CHARSET=utf8mb4
# SQLite specific
PDODB_PATH=/path/to/database.sqlite
# Paths
PDODB_MIGRATION_PATH=/path/to/migrations
PDODB_MODEL_PATH=/path/to/modelsCreate a .env file in your project root:
PDODB_DRIVER=mysql
PDODB_HOST=localhost
PDODB_PORT=3306
PDODB_DATABASE=mydb
PDODB_USERNAME=user
PDODB_PASSWORD=password
PDODB_CHARSET=utf8mb4
PDODB_MIGRATION_PATH=./database/migrations
PDODB_MODEL_PATH=./app/ModelsGenerate database migrations with interactive prompts and helpful suggestions.
# Interactive mode (will prompt for migration name)
vendor/bin/pdodb migrate create
# Non-interactive mode
vendor/bin/pdodb migrate create create_users_table$ vendor/bin/pdodb migrate create create_users_table
PDOdb Migration Generator
Database: mysql
Migrations path: /path/to/migrations
Suggested migration types:
1. create_table
0. Custom (manual)
Select migration type [0]: 1
ℹ Selected: create_table
✓ Migration file created: m2024_01_15_123456_create_users_table.php
Path: /path/to/migrations/m2024_01_15_123456_create_users_table.php<?php
declare(strict_types=1);
namespace tommyknocker\pdodb\migrations;
/**
* Migration: create_users_table
*
* Created: 2024_01_15_123456
*/
class m20240115123456CreateUsersTable extends Migration
{
/**
* {@inheritDoc}
*/
public function up(): void
{
// TODO: Implement migration up logic
}
/**
* {@inheritDoc}
*/
public function down(): void
{
// TODO: Implement migration down logic
}
}Generate ActiveRecord model classes from existing database tables.
# Auto-detect table name from model name
vendor/bin/pdodb model make User
# Specify table name explicitly
vendor/bin/pdodb model make User users
# Specify output path
vendor/bin/pdodb model make User users app/Models$ vendor/bin/pdodb model make User
PDOdb Model Generator
Database: mysql
Enter table name [users]: users
✓ Model file created: User.php
Path: /path/to/models/User.php
Table: users
Primary key: id<?php
declare(strict_types=1);
namespace App\Models;
use tommyknocker\pdodb\orm\Model;
/**
* Model class for table: users
*
* Auto-generated by PDOdb Model Generator
*/
class User extends Model
{
/**
* {@inheritDoc}
*/
public static function tableName(): string
{
return 'users';
}
/**
* {@inheritDoc}
*/
public static function primaryKey(): array
{
return ['id'];
}
/**
* Model attributes.
*
* @var array<string, mixed>
*/
public array $attributes = [
'id' => null,
'name' => null,
'email' => null,
'created_at' => null,
];
/**
* {@inheritDoc}
*/
public static function relations(): array
{
return [
// TODO: Define relationships based on foreign keys
];
}
}Inspect database schema structure, including tables, columns, indexes, foreign keys, and constraints.
# List all tables
vendor/bin/pdodb schema inspect
# Inspect specific table
vendor/bin/pdodb schema inspect users
# Output in JSON format
vendor/bin/pdodb schema inspect users --format=json
# Output in YAML format
vendor/bin/pdodb schema inspect users --format=yaml$ vendor/bin/pdodb schema inspect users
PDOdb Schema Inspector
Database: mysql
Table: users
============================================================
Columns:
------------------------------------------------------------
Name Type Nullable Default
------------------------------------------------------------
id int NO NULL
name varchar(100) NO NULL
email varchar(255) NO NULL
created_at timestamp YES CURRENT_TIMESTAMP
Indexes:
------------------------------------------------------------
PRIMARY (id)
idx_users_email UNIQUE (email)
Foreign Keys:
------------------------------------------------------------$ vendor/bin/pdodb schema inspect users --format=json
{
"table": "users",
"columns": [
{
"name": "id",
"type": "int",
"nullable": false,
"default": null
},
...
],
"indexes": [...],
"foreign_keys": [...],
"constraints": [...]
}Interactive REPL for testing SQL queries against the database.
# Interactive mode
vendor/bin/pdodb query test
# Execute single query
vendor/bin/pdodb query test "SELECT * FROM users LIMIT 10"exit,quit,q- Exit the query testerhelp- Show help messageclear,cls- Clear the screenhistory- Show query history
$ vendor/bin/pdodb query test
PDOdb Query Tester (REPL)
Database: mysql
Type 'exit' or 'quit' to exit, 'help' for help
pdodb> SELECT * FROM users LIMIT 5
id name email created_at
------------------------------------------------------------
1 John Doe [email protected] 2024-01-15 10:00:00
2 Jane Smith [email protected] 2024-01-15 10:01:00
3 Bob Johnson [email protected] 2024-01-15 10:02:00
4 Alice Brown [email protected] 2024-01-15 10:03:00
5 Charlie Wilson [email protected] 2024-01-15 10:04:00
Total rows: 5
pdodb> SELECT COUNT(*) FROM users
COUNT(*)
------------------------------------------------------------
100
Total rows: 1
pdodb> exit
Goodbye!After installing PDOdb via Composer, the CLI tool is automatically available in vendor/bin/:
composer require tommyknocker/pdo-database-classPDOdb provides a unified CLI tool with command-based structure (similar to Yii2):
vendor/bin/pdodb <command> [subcommand] [arguments] [options]migrate- Manage database migrationsschema- Inspect database schemaquery- Test SQL queries interactivelymodel- Generate ActiveRecord models
# Show all available commands
vendor/bin/pdodb
# Show help for a specific command
vendor/bin/pdodb migrate --help
vendor/bin/pdodb schema --help
vendor/bin/pdodb query --help
vendor/bin/pdodb model --help- Use
.envfiles for configuration in production environments - Keep migrations organized in a dedicated directory
- Review generated code before committing to version control
- Use schema inspector to verify table structures before writing queries
- Use query tester for quick query debugging and testing
Ensure you have:
- A
.envfile in the current working directory, OR - A
config.phpfile in the current working directory, OR - Environment variables set (
PDODB_DRIVER, etc.)
Set the PDODB_MIGRATION_PATH environment variable or create a migrations directory in your project root.
Set the PDODB_MODEL_PATH environment variable or create a models directory in your project root.
Verify that:
- The database connection is configured correctly
- You have the necessary permissions to query the database
- The database contains tables