1. Go to this page and download the library: Download alexya-framework/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/ */
alexya-framework / database example snippets
$Database = new \Alexya\Database\Connection("localhost", 3306, "root", "", "alexya");
$users = $Database->execute("SELECT * FROM `users`");
print_r($users);
$query->insert("users")
->values([
"login_log" => ["date1", "date2", "date3"]
]); // INSERT INTO `users` (`login_log`) VALUES ('a:3:{i:0;s:5:"date1";i:1;s:5:"date2";i:2;s:5:"date3";}')
$log = new LoginLog(); //Let's assume it exists and is the same as the array but in an object shape
$query->insert("users")
->values([
"login_log" => $log
]); // INSERT INTO `users` (`login_log`) VALUES ('O:3:"Obj":3:{s:5:"date1";s:5:"date1";s:5:"date2";s:5:"date2";s:5:"date3";s:5:"date3";}')
$query->insert("users")
->values([
"(JSON)login_log" => ["date1", "date2", "date3"]
]); // INSERT INTO `users` (`login_log`) VALUES ('["date1","date2","date3"]')
$log = new LoginLog(); //Let's assume it exists and is the same as the array but in an object shape
$query->insert("users")
->values([
"(JSON)login_log" => $log
]); // INSERT INTO `users` (`login_log`) VALUES ('{"date1":"date1","date2":"date2","date3":"date3"}')
$query->sql("SELECT * FROM users WHERE username='test'"); //SELECT * FROM users WHERE username='test'
$connection = new Connection("localhost", 3306, "root", "", "alexya");
Model::initialize($connection, "\Application\ORM");
class UsersTable extends Model
{
protected $_table = "users"; // Without this, the table name would be `userstables`, see \Alexya\Database\ORM\Model::getTable
}
class UsersTable extends Model
{
protected $_primaryKey = "userID";
}
$user = UsersTable::find(1); // SELECT * FROM `users` WHERE `usersID`=1
$otherUser = UsersTable::find([
"name" => "test"
]); // SELECT * FROM `users` WHERE `name`='test'
if($user->name == $otherUser->name) {
$user->name = "Test";
$user->save;
$otherUser->id = 2;
$otherUser->save;
}
$user = UsersTable::find(1); // SELECT * FROM `users` WHERE `usersID`=1
$user->name = "test";
$user->password = "test";
$user->save(); // UPDATE `users` SET `name`='test', `password`='test' WHERE `usersID`=1
$user = UsersTable::find(1); // SELECT * FROM `users` WHERE `usersID`=1
$user->delete(); // DELETE FROM `users` WHERE `userID`=1
class User extends Model
{
protected static $_relations = [
"Message" => [
"type" => "has_many"
],
"Configuration"
]
}
class User extends Model
{
protected static $_relations = [
"Message" => [
"type" => "has_many",
"foreignKey" => "to_users_id"
],
"Configuration"
]
}
class User extends Model
{
protected static $_relations = [
"Message" => [
"type" => "has_many",
"foreignKey" => "to_users_id",
"name" => "Messages"
],
"Configuration"
]
}
class User extends Model
{
protected static $_relations = [
"Message" => [
"type" => "has_many",
"foreignKey" => "to_users_id",
"name" => "Messages",
"condition" => "User::canSetMessage"
],
"Configuration"
]
public static function canSetMessage(array $columns) : bool
{
// Check that the user has verified his email
if(!$this->email_isVerified) {
return false;
}
// Check that the message isn't deleted by the user
if($columns["to_isDeleted"]) {
return false;
}
return true;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.