A Symfony bundle for IBM AS400/DB2 database connections with ORM-like functionality using PHP attributes.
Add the bundle to your project:
composer require cisse/as400-bundle
Register the bundle in config/bundles.php:
return [
// ...
Cisse\Bundle\As400\As400Bundle::class => ['all' => true],
];
Configure the bundle in config/packages/as400.yaml:
as400:
connection:
driver: "IBM i Access ODBC Driver" # Your ODBC driver name
system: "your-as400-system" # AS400 system name/IP
user: "your-username" # AS400 username
password: "your-password" # AS400 password
commit_mode: 0 # Optional, default: 0
extended_dynamic: 1 # Optional, default: 1
package_library: "QGPL" # Optional, default: "QGPL"
translate_hex: "1" # Optional, default: "1"
database: "" # Optional
default_libraries: "LIB1,LIB2" # Optional, comma-separated
generator: # Optional: Configure entity generation paths
entity_dir: "src/Entity/As400" # Default: src/Entity/As400
repository_dir: "src/Repository/As400" # Default: src/Repository/As400
test_dir: "tests/Entity/As400" # Default: tests/Entity/As400
entity_namespace: "App\\Entity\\As400" # Default: App\Entity\As400
repository_namespace: "App\\Repository\\As400" # Default: App\Repository\As400
test_namespace: "App\\Tests\\Entity\\As400" # Default: App\Tests\Entity\As400
<?php
namespace App\Entity;
use Cisse\Bundle\As400\Attribute\Entity;
use Cisse\Bundle\As400\Attribute\Column;
#[Entity(table: 'CUSTOMERS', identifier: 'CUST_ID', database: 'MYLIB')]
class Customer
{
#[Column(name: 'CUST_ID', type: Column::INTEGER_TYPE)]
public ?int $id = null;
#[Column(name: 'CUST_NAME', type: Column::STRING_TYPE)]
public ?string $name = null;
#[Column(name: 'CREATED_DATE', type: Column::DATE_TYPE)]
public ?\DateTime $createdDate = null;
}
<?php
namespace App\Repository;
use App\Entity\Customer;
use Cisse\Bundle\As400\Repository\Repository;
class CustomerRepository extends Repository
{
protected const string ENTITY_CLASS = Customer::class;
}
<?php
namespace App\Controller;
use App\Repository\CustomerRepository;
class CustomerController
{
public function __construct(
private CustomerRepository $customerRepository
) {}
public function list()
{
// Find all customers
$customers = $this->customerRepository->findAll();
// Find by criteria
$activeCustomers = $this->customerRepository->findBy(['STATUS' => 'ACTIVE']);
// Find one customer
$customer = $this->customerRepository->find(123);
// Count customers
$count = $this->customerRepository->count(['STATUS' => 'ACTIVE']);
}
}
Use the console command to generate entities from database tables:
# Generate entity from table
php bin/console as400:generate:entity MYLIB CUSTOMERS
# Generate entity with repository
php bin/console as400:generate:entity MYLIB CUSTOMERS --with-repository
# Generate entity with PHPUnit test
php bin/console as400:generate:entity MYLIB CUSTOMERS --with-test
# Generate entity with both repository and test
php bin/console as400:generate:entity MYLIB CUSTOMERS --with-repository --with-test
# Generate with custom namespace
php bin/console as400:generate:entity MYLIB CUSTOMERS "App\\Entity\\AS400"
The --with-test option generates a PHPUnit test file that verifies:
Use this command to scan existing entities and generate missing tests and/or repositories:
# Generate all missing tests and repositories
php bin/console as400:generate:missing
# Generate only missing tests
php bin/console as400:generate:missing --tests
php bin/console as400:generate:missing -t
# Generate only missing repositories
php bin/console as400:generate:missing --repositories
php bin/console as400:generate:missing -r
# Preview what would be generated (dry run)
php bin/console as400:generate:missing --dry-run
The command will:
entity_dir<?php
use Cisse\Bundle\As400\Database\Connection\As400Connection;
class MyService
{
public function __construct(
private As400Connection $connection
) {}
public function customQuery()
{
// Raw queries
$results = $this->connection->fetchAll('SELECT * FROM MYLIB.CUSTOMERS WHERE STATUS = ?', ['ACTIVE']);
// CRUD operations
$this->connection->insert('MYLIB.CUSTOMERS', ['NAME' => 'John Doe', 'STATUS' => 'ACTIVE']);
$this->connection->update('MYLIB.CUSTOMERS', ['STATUS' => 'INACTIVE'], ['CUST_ID' => 123]);
$this->connection->delete('MYLIB.CUSTOMERS', ['CUST_ID' => 123]);
}
}
The bundle includes a data collector for the Symfony Web Profiler toolbar, showing:
Available column types for attribute mapping:
Column::STRING_TYPEColumn::INTEGER_TYPEColumn::BOOLEAN_TYPEColumn::FLOAT_TYPEColumn::DATE_TYPEColumn::DATETIME_TYPEColumn::TIME_TYPEMIT
How can I help you explore Laravel packages today?