PHP code example of ody / database

1. Go to this page and download the library: Download ody/database 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/ */

    

ody / database example snippets


// config/database.php
return [
    'environments' => [
        'local' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'port' => 3306,
            'database' => 'your_database',
            'username' => 'your_username',
            'password' => 'your_password',
            'charset' => 'utf8mb4',
        ],
    ],
    'connection_pool_enabled' => true,
    'pool_size' => 10,
];

use Ody\DB\Doctrine\Facades\ORM;
use App\Entities\User;

// Get entity manager
$entityManager = ORM::entityManager();

// Working with entities
$user = $entityManager->find(User::class, 1);
$entityManager->persist($user);
$entityManager->flush();

use Ody\DB\Doctrine\Facades\DBAL;

// Execute queries
$users = $this->connection->fetchAllAssociative('SELECT * FROM users WHERE active = ?', [1]);

// Using query builder
$queryBuilder = $this->connection->createQueryBuilder();
$result = $queryBuilder
    ->select('u.*')
    ->from('users', 'u')
    ->where('u.active = :active')
    ->setParameter('active', 1)
    ->executeQuery()
    ->fetchAllAssociative();

use Ody\DB\ConnectionManager;

// Initialize the pool
ConnectionManager::initPool($config);

// Get a connection from the pool
$connection = ConnectionManager::getConnection();

// Use the PDO connection
$stmt = $connection->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([1]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// No need to return the connection - it's automatically returned when the coroutine ends