PHP code example of vitexsoftware / ease-fluentpdo
1. Go to this page and download the library: Download vitexsoftware/ease-fluentpdo 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/ */
vitexsoftware / ease-fluentpdo example snippets
onfigure database connection
define('DB_TYPE', 'mysql');
define('DB_HOST', 'localhost');
define('DB_DATABASE', 'myapp');
define('DB_USERNAME', 'user');
define('DB_PASSWORD', 'password');
// Create a database-backed object
class User extends \Ease\SQL\Engine {
public function __construct($identifier = null) {
$this->myTable = 'users';
$this->keyColumn = 'id';
parent::__construct($identifier);
}
}
// Usage example
$user = new User();
$user->setDataValue('name', 'John Doe');
$user->setDataValue('email', '[email protected]');
$user->save(); // Inserts or updates the record
// Create a model class
class Article extends \Ease\SQL\Engine {
public function __construct($identifier = null) {
$this->myTable = 'articles';
$this->keyColumn = 'id';
parent::__construct($identifier);
}
}
// Create new record
$article = new Article();
$article->setDataValue('title', 'My First Article');
$article->setDataValue('content', 'This is the content...');
$article->setDataValue('author_id', 1);
$savedId = $article->save();
// Load existing record
$article = new Article(1);
echo $article->getDataValue('title');
// Update record
$article->setDataValue('title', 'Updated Title');
$article->save();
// Delete record
$article->delete();
$engine = new \Ease\SQL\Engine();
// Select with conditions
$users = $engine->listingQuery()
->from('users')
->where('active = ?', 1)
->where('created_at > ?', '2023-01-01')
->orderBy('name ASC')
->fetchAll();
// Complex joins
$articles = $engine->listingQuery()
->from('articles a')
->leftJoin('users u ON a.author_id = u.id')
->select('a.*, u.name as author_name')
->where('a.published = ?', 1)
->fetchAll();