Add more examples

This commit is contained in:
Daniil Gentili 2024-03-31 18:08:04 +02:00
parent 4306bbe24f
commit d526ae4e4d
3 changed files with 114 additions and 4 deletions

View File

@ -1,16 +1,39 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
use Amp\Mysql\MysqlConfig; use Amp\Mysql\MysqlConfig;
use Amp\Postgres\PostgresConfig;
use Amp\Redis\RedisConfig;
use danog\AsyncOrm\DbObject; use danog\AsyncOrm\DbObject;
use danog\AsyncOrm\FieldConfig; use danog\AsyncOrm\FieldConfig;
use danog\AsyncOrm\KeyType; use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\Settings\MysqlSettings; use danog\AsyncOrm\Settings\MysqlSettings;
use danog\AsyncOrm\Settings\PostgresSettings;
use danog\AsyncOrm\Settings\RedisSettings;
use danog\AsyncOrm\ValueType; use danog\AsyncOrm\ValueType;
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
$settings = new MysqlSettings( $settings = new MysqlSettings(
new MysqlConfig("/var/run/mysqld/mysqld.sock", 0, 'daniil', database: 'test'), 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
); );
$fieldConfig = new FieldConfig( $fieldConfig = new FieldConfig(

87
examples/2-automatic.php Normal file
View File

@ -0,0 +1,87 @@
<?php declare(strict_types=1);
use Amp\Mysql\MysqlConfig;
use Amp\Postgres\PostgresConfig;
use Amp\Redis\RedisConfig;
use danog\AsyncOrm\Annotations\OrmMappedArray;
use danog\AsyncOrm\DbArray;
use danog\AsyncOrm\DbAutoProperties;
use danog\AsyncOrm\FieldConfig;
use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\Settings;
use danog\AsyncOrm\Settings\MysqlSettings;
use danog\AsyncOrm\Settings\PostgresSettings;
use danog\AsyncOrm\Settings\RedisSettings;
use danog\AsyncOrm\ValueType;
require __DIR__ . '/../vendor/autoload.php';
$settings = new MysqlSettings(
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
);
$fieldConfig = new FieldConfig(
'tableName',
$settings,
KeyType::STRING,
ValueType::OBJECT
);
$db = $fieldConfig->build();
/**
* Main class of your application.
*/
final class Application
{
use DbAutoProperties;
/**
* This field is automatically connected to the database using the specified Settings.
*/
#[OrmMappedArray(KeyType::STRING, ValueType::INT)]
private DbArray $dbProperty;
public function __construct(
Settings $settings,
string $tablePrefix
) {
$this->initDbProperties($settings, $tablePrefix);
}
public function businessLogic(): void
{
$this->dbProperty['someKey'] = 123;
var_dump($this->dbProperty['someKey']);
}
public function shutdown(): void
{
// Flush all database caches, saving all changes.
$this->saveDbProperties();
}
}
$app = new Application($settings, 'tablePrefix');
$app->businessLogic();
$app->shutdown();

View File

@ -20,10 +20,10 @@ use Attribute;
use danog\AsyncOrm\KeyType; use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\ValueType; use danog\AsyncOrm\ValueType;
/** /**
* Attribute use to autoconfigure ORM properties. * Attribute use to autoconfigure ORM properties.
* *
* @api * @api
*/ */
#[Attribute(Attribute::TARGET_PROPERTY)] #[Attribute(Attribute::TARGET_PROPERTY)]
final class OrmMappedArray final class OrmMappedArray