PHP code example of clancats / hydrahon

1. Go to this page and download the library: Download clancats/hydrahon 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/ */

    

clancats / hydrahon example snippets

 
$connection = new PDO('mysql:host=localhost;dbname=my_database;charset=utf8', 'username', 'password');

// create a new mysql query builder
$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
{
    $statement = $connection->prepare($queryString);
    $statement->execute($queryParameters);

    // when the query is fetchable return all results and let hydrahon do the rest
    // (there's no results to be fetched for an update-query for example)
    if ($query instanceof \ClanCats\Hydrahon\Query\Sql\FetchableInterface)
    {
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
    }
    // when the query is a instance of a insert return the last inserted id  
    elseif($query instanceof \ClanCats\Hydrahon\Query\Sql\Insert)
    {
        return $connection->lastInsertId();
    }
    // when the query is not a instance of insert or fetchable then
    // return the number os rows affected
    else 
    {
        return $statement->rowCount();
    }	
});

// In our example we are going to execute multiple operations on the same table, 
// so instead of loading the table over and over again, we store it in a variable.
$people = $h->table('people');

$people->insert(
[
    ['name' => 'Ray', 'age' => 25],
    ['name' => 'John',  'age' => 30],
    ['name' => 'Ali', 'age' => 22],
])->execute();

$people->update()
    ->set('age', 26)
    ->where('name', 'Ray')
->execute();

$people->delete()
    ->where('name', 'John')
->execute();

$people->select()->get();

// select * from `people` where `age` = 21 and `name` like 'J%'
$people->select()
    ->where('age', 21)
    ->where('name', 'like', 'J%')
    ->get();

// select * from `people` where `name` like 'J%' or `name` like 'I%'
$people->select()
    ->where('name', 'like', 'J%')
    ->orWhere('name', 'like', 'I%')
    ->get();

// select * from `people` where ( `age` > 21 and `age` < 99 ) or `group` = admin
$people->select()
    ->where(function($q) 
    {
        $q->where('age', '>', 21);
        $q->where('age', '<', 99);
    })
    ->orWhere('group', 'admin')
    ->get();

// select 
//     `people`.`name`, `groups`.`name` as `group_name` 
// from `people` 
// left join `groups` on `groups`.`id` = `people`.`group_id`
$people->select('people.name, groups.name as group_name')
    ->join('groups', 'groups.id', '=', 'people.group_id')
    ->get();

// select * from `people` group by `age`
$people->select()->groupBy('age')->get();

// select * from `people` order by `age` desc
$people->select()->orderBy('age', 'desc')->get();

// select * from `people` order by `age` desc, `name` asc
$people->select()->orderBy(['age' => 'desc', 'name' => 'asc'])->get();

// select * from `people` limit 0, 10
$people->select()->limit(10)->get();

// select * from `people` limit 100, 10
$people->select()->limit(100, 10)->get();

// select * from `people` limit 100, 10
$people->select()->limit(10)->offset(100)->get();

// select * from `people` limit 150, 30
$people->select()->page(5, 30)->get();
sql
insert into `people` (`age`, `name`) values (?, ?), (?, ?), (?, ?)