PHP code example of yangweijie / es-think-orm

1. Go to this page and download the library: Download yangweijie/es-think-orm 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/ */

    

yangweijie / es-think-orm example snippets


// config/database.php
return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'type' => 'mysql',
            'hostname' => '127.0.0.1',
            'database' => 'test',
            'username' => 'root',
            'password' => '',
            'hostport' => '3306',
            'charset' => 'utf8mb4',
            'debug' => true,
            'pool' => [
                'min_active' => 2,
                'max_active' => 10,
                'max_wait_time' => 5,
                'max_idle_time' => 60,
            ],
            'cache' => [
                'class' => 'EasyOrm\FileCacheHandler',
                'params' => [
                    'cache_dir' => EASYSWOOLE_ROOT . '/Temp/cache',
                ],
            ],
        ],
    ],
];

// config/database.php
return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            // ... database config ...
            'cache' => [
                'class' => 'EasyOrm\RedisCacheHandler',
                'params' => [
                    'host' => '127.0.0.1',
                    'port' => 6379,
                    'auth' => '',           // Redis password (optional)
                    'db_index' => 0,        // Redis database index
                    'timeout' => 3.0,       // Connection timeout
                ],
            ],
        ],
    ],
];

use EasyOrm\EasyDb;

// Create custom cache handler
class MyCacheHandler
{
    public function get($key)
    {
        // Return cached value or null
    }

    public function set($key, $value, $ttl = null)
    {
        // Store value, return bool
    }

    public function rm($key)
    {
        // Remove cached value, return bool
    }
}

// Set cache handler manually
EasyDb::setCacheHandler(new MyCacheHandler());

use EasyOrm\EasyDb;

public static function initialize()
{
    $config = 

use EasyOrm\EasyDb;

// Basic query
$users = EasyDb::name('user')->where('status', 1)->select();

// With cache
$users = EasyDb::name('user')->cache(true, 60)->where('status', 1)->select();

// Transaction
EasyDb::transaction(function() {
    EasyDb::name('user')->insert(['name' => 'test']);
    EasyDb::name('order')->insert(['user_id' => 1]);
});