1. Go to this page and download the library: Download aternos/model 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/ */
aternos / model example snippets
$driver = new \Aternos\Model\Driver\Mysqli\Mysqli("localhost", 3306, "username", "password");
$driver = (new \Aternos\Model\Driver\Mysqli\Mysqli())->setUsername("username")->setPassword("password");
\Aternos\Model\Driver\DriverRegistry::getInstance()->registerDriver($driver);
// or using your own class
\Aternos\Model\Driver\DriverRegistry::getInstance()->registerDriverClass(\MyModel\Mysqli::ID, \MyModel\Mysqli::class);
$driverA = (new \Aternos\Model\Driver\Mysqli\Mysqli())->setId("mysqli-a")->setHost("a.mysql.host");
\Aternos\Model\Driver\DriverRegistry::getInstance()->registerDriver($driverA);
$driverB = (new \Aternos\Model\Driver\Mysqli\Mysqli())->setId("mysqli-b")->setHost("b.mysql.host");
\Aternos\Model\Driver\DriverRegistry::getInstance()->registerDriver($driverB);
class User extends \Aternos\Model\GenericModel
{
// use model registry (default: true)
protected static bool $registry = true;
// cache the model for 60 seconds (default: null)
protected static ?int $cache = 60;
// configure the generic model drivers (this is the default)
protected static array $drivers = [
\Aternos\Model\Driver\Redis\Redis::ID,
\Aternos\Model\Driver\Mysqli\Mysqli::ID
];
// the name of your model (and table)
public static function getName() : string
{
return "users";
}
// all public properties are database fields
public $id;
public $username;
public $email;
}
// create new user
$user = new User();
$user->username = "username";
$user->email = "[email protected]";
$user->save();
// get a user by id
$user = User::get($id);
echo $user->username;
// you can force to skip the registry and the cache
$user = User::get($id, true);
// update a user
$user->email = "[email protected]";
$user->save();
// delete a user
$user->delete();
// the following lines are all the same
User::select(["email" => "[email protected]"]); // ::select() is only a helper function of GenericModel
User::query(new \Aternos\Model\Query\SelectQuery(["email" => "[email protected]"]));
User::query((new \Aternos\Model\Query\SelectQuery())->where(["email" => "[email protected]"]));
User::query(new \Aternos\Model\Query\SelectQuery(
new \Aternos\Model\Query\WhereCondition("email", "[email protected]")
));
User::query(new \Aternos\Model\Query\SelectQuery(
new \Aternos\Model\Query\WhereGroup([
new \Aternos\Model\Query\WhereCondition("email", "[email protected]")
])
));
// use the result
$userQueryResult = User::select(["email" => "[email protected]"]);
if (!$userQueryResult->wasSuccessful()) {
echo "Query failed";
}
echo "Found " . count($userQueryResult) . " users";
foreach($userQueryResult as $user) {
/** @var User $user */
echo $user->username;
}
// another query example
User::select(
["field" => "value", "hello" => "world", "foo" => "bar"],
["field" => "ASC", "hello" => "DESC", "foo" => "ASC"],
["field", "hello", "foo"],
[100, 10]
);
// can also be written as
User::query((new \Aternos\Model\Query\SelectQuery)
->where(["field" => "value", "hello" => "world", "foo" => "bar"])
->orderBy(["field" => "ASC", "hello" => "DESC", "foo" => "ASC"])
->fields(["field", "hello", "foo"])
->limit([100, 10])
);
// a more complex query with nested where groups using the query parameter classes
User::query(new \Aternos\Model\Query\SelectQuery(
new \Aternos\Model\Query\WhereGroup([
new \Aternos\Model\Query\WhereCondition("field", "value", "<>"),
new \Aternos\Model\Query\WhereGroup([
new \Aternos\Model\Query\WhereCondition("hello", "world"),
new \Aternos\Model\Query\WhereCondition("foo", "bar")
], \Aternos\Model\Query\WhereGroup:: OR)
]),
[
new \Aternos\Model\Query\OrderField("field", \Aternos\Model\Query\OrderField::DESCENDING),
new \Aternos\Model\Query\OrderField("hello", \Aternos\Model\Query\OrderField::ASCENDING),
new \Aternos\Model\Query\OrderField("foo", \Aternos\Model\Query\OrderField::DESCENDING)
],
[
new \Aternos\Model\Query\SelectField("field"),
new \Aternos\Model\Query\SelectField("hello"),
new \Aternos\Model\Query\SelectField("foo")
],
new \Aternos\Model\Query\Limit(10, 100)
));
// update mail to "[email protected]" where username is "username"
User::query(new \Aternos\Model\Query\UpdateQuery(["email" => "[email protected]"], ["username" => "username"]));
User::query((new \Aternos\Model\Query\UpdateQuery())
->fields(["email" => "[email protected]"])
->where(["username" => "username"]));
User::query(new \Aternos\Model\Query\UpdateQuery(
new \Aternos\Model\Query\UpdateField("email", "[email protected]"),
new \Aternos\Model\Query\WhereCondition("username", "username")
));
User::query(new \Aternos\Model\Query\UpdateQuery(
[new \Aternos\Model\Query\UpdateField("email", "[email protected]")],
new \Aternos\Model\Query\WhereGroup([
new \Aternos\Model\Query\WhereCondition("username", "username")
])
));
// delete where email is [email protected]
User::query(new \Aternos\Model\Query\DeleteQuery(["email" => "[email protected]"]));
User::query((new \Aternos\Model\Query\DeleteQuery())->where(["email" => "[email protected]"]));
User::query(new \Aternos\Model\Query\DeleteQuery(
new \Aternos\Model\Query\WhereCondition("email", "[email protected]")
));
User::query(new \Aternos\Model\Query\DeleteQuery(
new \Aternos\Model\Query\WhereGroup([
new \Aternos\Model\Query\WhereCondition("email", "[email protected]")
])
));
// add a single entry
User::addTestEntry([
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
]);
// add multiple entries at once
User::addTestEntries($entries);
// clear all test entries
User::clearTestEntries();
/** @var \Aternos\Model\Driver\Test\TestDriver $testDriver */
$testDriver = \Aternos\Model\Driver\DriverRegistry::getInstance()->getDriver(\Aternos\Model\Driver\Test\TestDriver::ID);
// add multiple tables at once
$testDriver->addTables([
"user" => [
[
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
],
...
],
"another_table" => [
[
"id" => 1,
...
]
],
...
]);
// add a single table
$testDriver->addTable("user", [
[
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
],
...
]);
// add an entry to a table
$testDriver->addEntry("user", [
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
]);
// clear all entries from a table
$testDriver->clearEntries("user");
// clear all tables
$testDriver->clearTables();
// enable the test driver for the user model
User::enableTestDriver();
// you can enable the test driver for all models at once by enabling it on a shared parent
class MyModel extends Aternos\Model\GenericModel {}
class User extends MyModel { ... }
class AnotherModel extends MyModel { ... }
MyModel::enableTestDriver();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.