AsyncOrm/examples/2-manual.php

61 lines
1.3 KiB
PHP
Raw Normal View History

2024-03-26 18:44:15 +01:00
<?php declare(strict_types=1);
use Amp\Mysql\MysqlConfig;
2024-03-31 18:08:04 +02:00
use Amp\Postgres\PostgresConfig;
use Amp\Redis\RedisConfig;
2024-03-26 18:44:15 +01:00
use danog\AsyncOrm\DbObject;
2024-03-31 19:11:42 +02:00
use danog\AsyncOrm\DbArrayBuilder;
2024-03-26 18:44:15 +01:00
use danog\AsyncOrm\KeyType;
2024-03-31 17:13:03 +02:00
use danog\AsyncOrm\Settings\MysqlSettings;
2024-03-31 18:08:04 +02:00
use danog\AsyncOrm\Settings\PostgresSettings;
use danog\AsyncOrm\Settings\RedisSettings;
2024-03-26 18:44:15 +01:00
use danog\AsyncOrm\ValueType;
require __DIR__ . '/../vendor/autoload.php';
2024-03-31 19:11:42 +02:00
// Any of the following database backends can be used,
// remove the ones you don't need.
2024-03-31 17:13:03 +02:00
$settings = new MysqlSettings(
2024-03-31 18:08:04 +02:00
new MysqlConfig(
host: "/var/run/mysqld/mysqld.sock",
user: 'user',
password: 'password',
database: 'database'
),
cacheTtl: 100
);
$settings = new PostgresSettings(
new PostgresConfig(
host: "127.0.0.1",
user: "user",
password: "password",
database: "database"
),
cacheTtl: 100
);
$settings = new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 100
2024-03-26 18:44:15 +01:00
);
2024-03-31 19:11:42 +02:00
$fieldConfig = new DbArrayBuilder(
2024-03-26 18:44:15 +01:00
'tableName',
$settings,
KeyType::STRING,
ValueType::OBJECT
);
$db = $fieldConfig->build();
class MyObject extends DbObject
{
2024-03-26 20:33:42 +01:00
public function __construct(
public readonly string $value
) {
}
2024-03-26 18:44:15 +01:00
}
2024-03-26 20:33:42 +01:00
$db->set("a", new MyObject('v'));
$obj = $db->get("a");
var_dump($obj->value);