Recommend this page to a friend! |
![]() |
Info | Documentation | ![]() |
![]() |
![]() |
Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 132 | All time: 9,329 This week: 455![]() |
Version | License | PHP version | Categories | |||
queasy-db 1.0.1 | Custom (specified... | 5 | PHP 5, Databases |
v-dem/queasy-db
Database access classes. Some the most usual queries can be built automatically, more complex queries can be added into database and/or tables config.
composer require v-dem/queasy-db:master-dev
Sample:
$db = new queasy\db\Db(
[
'connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'name' => 'test',
'user' => 'test_user',
'password' => 'test_password'
],
'fetchMode' => PDO::FETCH_ASSOC // Default fetch mode for all queries
]
);
Or
$db = new queasy\db\Db(
[
'connection' => [
'dsn' => 'mysql:host=localhost;dbname=test',
'user' => 'test_user',
'password' => 'test_password'
],
'fetchMode' => PDO::FETCH_ASSOC // Default fetch mode for all queries
]
);
Or PDO-way:
$db = new queasy\db\Db('mysql:host=localhost;dbname=test', 'test_user', 'test_password');
users
table$user = $db->users->all();
users
table by id
key$user = $db->users->id[$userId];
It will generate the following query:
SELECT *
FROM `users`
WHERE `id` = :id
It's possible to use select()
method to pass PDO options:
$user = $db->users->id->select($userId, $options);
$users = $db->users->id[[$userId1, $userId2]];
SQL:
SELECT *
FROM `users`
WHERE `id` IN (:id_1, :id_2)
users
table using associative array$db->users[] = [
'email' => 'john.doe@example.com',
'password_hash' => sha1('myverystrongpassword')
];
It will generate the following query:
INSERT INTO `users` (`email`, `password_hash`)
VALUES (:email, :password_hash)
users
table by fields order$db->users[] = [
'john.doe@example.com',
sha1('myverystrongpassword')
];
users
table using associative array (it will generate single INSERT
statement)$db->users[] = [
[
'email' => 'john.doe@example.com',
'password_hash' => sha1('myverystrongpassword')
], [
'email' => 'mary.joe@example.com',
'password_hash' => sha1('herverystrongpassword')
]
];
SQL:
INSERT INTO `users` (`email`, `password_hash`)
VALUES (:email_1, :password_hash_1),
(:email_2, :password_hash_2)
users
table by order$db->users[] = [
[
'john.doe@example.com',
sha1('myverystrongpassword')
], [
'mary.joe@example.com',
sha1('herverystrongpassword')
]
];
users
table with field names denoted separately$db->users[] = [
[
'email',
'password_hash'
], [
[
'john.doe@example.com',
sha1('myverystrongpassword')
], [
'mary.joe@example.com',
sha1('herverystrongpassword')
]
]
];
It's possible to use insert()
method to pass PDO options:
$db->users->insert([
'email' => 'john.doe@example.com',
'password_hash' => sha1('myverystrongpassword')
], $options);
lastInsertId()
method)$newUserId = $db->id();
users
table by id
key$db->users->id[$userId] = [
'password_hash' => sha1('mynewverystrongpassword')
]
$db->users->id[[$userId1, $userId2]] = [
'is_blocked' => true
]
users
table by id
keyunset($db->users->id[$userId]);
unset($db->users->id[[$userId1, $userId2]]);
users
table$usersCount = count($db->users);
$db->trans(function(queasy\db\Db $db) use(...) {
// Run queries inside a transaction
});
foreach
with a users
tableforeach($db->users as $user) {
// Do something
}
PDOStatement
)$result = $db->run('
SELECT *
FROM `users`
WHERE `name` LIKE concat(\'%\', :searchName, \'%\')',
[
':searchName' => $searchName
]
);
This feature can help keep code cleaner and place SQL code outside PHP, somewhere in config files.
$db = new queasy\db\Db(
[
'connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'name' => 'test',
'user' => 'test_user',
'password' => 'test_password'
],
'fetchMode' => PDO::FETCH_ASSOC,
'queries' => [
'selectUserRoleByName' => [
'sql' => '
SELECT *
FROM `user_roles`
WHERE `name` = :name',
'returns' => Db::RETURN_ONE
]
]
]
);
$role = $db->selectUserRoleByName(['name' => 'Manager']);
Also it is possible to group predefined queries by tables:
$db = new queasy\db\Db(
[
'connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'name' => 'test',
'user' => 'test_user',
'password' => 'test_password'
],
'fetchMode' => PDO::FETCH_ASSOC,
'tables' => [
`user_roles` => [
`queries` => [
'selectUserRoleByName' => [
'sql' => '
SELECT *
FROM `user_roles`
WHERE `name` = :name',
'returns' => Db::RETURN_ONE
]
]
]
]
]
);
$role = $db->user_roles->selectUserRoleByName(['name' => 'Manager']);
v-dem/queasy-db
together with v-dem/queasy-config
$config = new queasy\config\Config('config.php'); // Can be also INI, JSON or XML
$db = new queasy\db\Db($config->db);
config.php:
return [
'db' => [
'connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'name' => 'test',
'user' => 'test_user',
'password' => 'test_password'
],
'fetchMode' => PDO::FETCH_ASSOC,
'tables' => [
'user_roles' => [
'queries' => [
'selectUserRoleByName' => [
'sql' => '
SELECT *
FROM `user_roles`
WHERE `name` = :name',
'returns' => Db::RETURN_ONE
]
]
]
]
]
];
v-dem/queasy-db
together with v-dem/queasy-log
$config = new queasy\config\Config('config.php');
$logger = new queasy\log\Logger($config->logger);
$db = new queasy\db\Db($config->db);
$db->setLogger($config->logger);
![]() |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Lic. | License text | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Doc. | Documentation |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
![]() |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.