Trabla: php-activerecord ( php ORM ): get PDO object to execute query directly
Solving:
use ActiveRecord\ConnectionManager;
...
$pdo = ConnectionManager::get_connection()->connection;
Example:
...
use ActiveRecord\ConnectionManager;
....
function getUsersByName( $name ){
$activeRecordAdapter = ConnectionManager::get_connection();
$pdo = $activeRecordAdapter->connection;
// Or just
// $pdo = ConnectionManager::get_connection()->connection;
// :)
$sql = ' SELECT id, name FROM tbl_users WHERE name = :name ';
$query = $pdo->prepare( $sql );
$query->execute( array( ':name' => $name ) );
$rows = $query->fetchAll($pdo::FETCH_ASSOC);
unset($query);
unset($pdo);
return $rows;
}
function getUsers(){
$activeRecordAdapter = ConnectionManager::get_connection();
$pdo = $activeRecordAdapter->connection;
// Or just
// $pdo = ConnectionManager::get_connection()->connection;
// :)
$sql = ' SELECT id, name FROM tbl_users ';
$query = $pdo->prepare( $sql );
$query->execute();
$rows = $query->fetchAll($pdo::FETCH_ASSOC);
unset($query);
unset($pdo);
return $rows;
}
No comments:
Post a Comment