| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Effectra\Database is a PHP package that provides database connection and query execution functionality. It offers a convenient interface for interacting with different database drivers and executing common database operations.
You can install the Effectra\Database package via Composer. Simply run the following command:
composer require effectra/dbTo establish a database connection, you need to create an instance of the Connection class and call the connect method. The connect method retrieves the database configuration from the provided configuration file and returns a PDO object representing the database connection.
use Effectra\Database\Connection;
use Effectra\Database\Diver;
use Effectra\Config\ConfigDB;
// Create a new instance of the Connection class
$mysqlConfig = [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your_database_name',
'username' => 'your_mysql_username',
'password' => 'your_mysql_password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
// Add any additional options if needed
];
$connection = new Connection($mysqlConfig);By default, the Connection class supports MySQL and SQLite database drivers. You can easily add support for additional database drivers by implementing the DriverInterface and configuring the Connection class accordingly.
Once you have established a database connection, you can execute queries using the DB class. The DB class provides methods for executing common database operations such as select, insert, update, and delete.
use Effectra\Database\DB;
// Establish the database connection
DB::createConnection($con);
// Establish the database event dispatcher
DB::setEventDispatcher(new EventDispatcher());
// Create a new instance of the DB class
$db = new DB();
// Execute a select query
$data = $db->withQuery('SELECT * FROM users')->get();
// Execute an insert query
$db->table('users')->data(['name' => 'Jane Doe','email'=> 'janeDoe@mail.com'])->insert();
// Execute an update query
$db->table('users')->data(['name' => 'Jane Doe'])->update((new Condition())->where(['id' => 2]));The DB class provides a fluent interface for building and executing queries. You can chain methods to construct complex queries easily.
If an error occurs during query execution, the DB class will throw a DatabaseException. You can catch and handle this exception to gracefully handle database errors.
use Effectra\Database\Exception\DatabaseException;
try {
$db->table('users')->insert( ['name' => 'John Doe']); // Missing 'email' field
} catch (DatabaseException $e) {
// Handle the exception
echo "Database Error: " . $e->getMessage();
}Namespace: The model is part of the Effectra\Database namespace.
Traits: The model uses the ModelEventTrait trait.
Properties:
Constructor:
Methods:
Overall, this model provides a flexible and extensible foundation for database interactions in a PHP application. It includes features for CRUD operations, query building, and transaction management. Additionally, it leverages traits for handling model events and uses Symfony's VarDumper for debugging purposes.
class User extends Model {
}$user = User::find(1);
print_r($user);
echo $user->id;
// or use method
echo $user->getId();$user->name = 'Foo Bar';
// or use method
$user->setName('Foo Bar');
$user->update();$user = new User();
$user->name = 'Foo Bar';
$user->email = 'FooBar@email.com';
// or use method
$user
->setName('Foo Bar');
->setEmail('FooBar@email.com');
$user->save();Contributions to the Effectra\Database package are welcome. If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on the GitHub repository.
The Effectra\Database package is open-source software licensed under the MIT license. See the LICENSE file for more information.
| Back | FazBrowse Home | New Git URL |