PHP code example of webvimark / pixie
1. Go to this page and download the library: Download webvimark/pixie 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' );
webvimark / pixie example snippets
'driver' => 'mysql' ,
'host' => 'localhost' ,
'database' => 'your-database' ,
'username' => 'root' ,
'password' => 'your-password' ,
'charset' => 'utf8' ,
'collation' => 'utf8_unicode_ci' ,
'prefix' => 'cb_' ,
'options' => array (
PDO::ATTR_TIMEOUT => 5 ,
PDO::ATTR_EMULATE_PREPARES => false ,
),
);
new \Pixie\Connection('mysql' , $config, 'QB' );
$row = QB::table('my_table' )->find(3 );
$query = QB::table('my_table' )->where('name' , '=' , 'Sana' );
$query->get();
QB::registerEvent('before-select' , 'users' , function ($qb)
{
$qb->where('status' , '!=' , 'banned' );
});
/ Db driver
'host' => 'localhost' ,
'database' => 'your-database' ,
'username' => 'root' ,
'password' => 'your-password' ,
'charset' => 'utf8' ,
'collation' => 'utf8_unicode_ci' ,
'prefix' => 'cb_' ,
);
new \Pixie\Connection('mysql' , $config, 'QB' );
$query = QB::table('my_table' )->where('name' , '=' , 'Sana' );
new \Pixie\Connection('mysql' , $config, 'MyAlias' );
$connection = new \Pixie\Connection('mysql' , $config);
$qb = new \Pixie\QueryBuilder\QueryBuilderHandler($connection);
$query = $qb->table('my_table' )->where('name' , '=' , 'Sana' );
var_dump($query->get());
new \Pixie\Connection('sqlite' , array (
'driver' => 'sqlite' ,
'database' => 'your-file.sqlite' ,
'prefix' => 'cb_' ,
), 'QB' );
new \Pixie\Connection('pgsql' , array (
'driver' => 'pgsql' ,
'host' => 'localhost' ,
'database' => 'your-database' ,
'username' => 'postgres' ,
'password' => 'your-password' ,
'charset' => 'utf8' ,
'prefix' => 'cb_' ,
'schema' => 'public' ,
), 'QB' );
QB::table(array ('mytable1' , 'mytable2' ));
$row = QB::table('my_table' )->find(3 );
$result = QB::table('my_table' )->findAll('name' , 'Sana' );
$query = QB::table('my_table' )->select('*' );
->select(array ('mytable.myfield1' , 'mytable.myfield2' , 'another_table.myfield3' ));
->selectDistinct(array ('mytable.myfield1' , 'mytable.myfield2' ));
$query = QB::table('my_table' )->where('name' , '=' , 'Sana' );
$result = $query->get();
foreach ($result as $row) {
echo $row['name' ];
}
$result = QB::table('my_table' )
->where('age' , '>' , '3' )
->map(function ($item) {
$item['age' ] = "{$item['name']} is {$item['age']} years old" ;
return $item;
})
->get()
$query = QB::table('my_table' )->where('name' , '=' , 'Sana' );
$row = $query->first();
$query = QB::table('my_table' )->where('name' , '=' , 'Sana' );
$query->count();
$firstColumn = QB::table('my_table' )->getColumn();
$specificColumn = QB::table('my_table' )->getColumn('name' );
$firstColumnValue = QB::table('my_table' )->getScalar();
$specificColumnValue = QB::table('my_table' )->getScalar('name' );
QB::table('my_table' )
->where('name' , '=' , 'usman' )
->whereNot('age' , '>' , 25 )
->orWhere('type' , '=' , 'admin' )
->orWhereNot('description' , 'LIKE' , '%query%' )
;
QB::table('my_table' )
->whereIn('name' , array ('usman' , 'sana' ))
->orWhereIn('name' , array ('heera' , 'dalim' ))
;
QB::table('my_table' )
->whereNotIn('name' , array ('heera' , 'dalim' ))
->orWhereNotIn('name' , array ('usman' , 'sana' ))
;
QB::table('my_table' )
->whereBetween('id' , 10 , 100 )
->orWhereBetween('status' , 5 , 8 );
QB::table('my_table' )
->whereNull('modified' )
->orWhereNull('field2' )
->whereNotNull('field3' )
->orWhereNotNull('field4' );
QB::table('my_table' )
->where('my_table.age' , 10 )
->where(function ($q)
{
$q->where('name' , 'LIKE' , '%usman%' );
$q->orWhere('description' , 'LIKE' , '%usman%' );
});
$query = QB::table('my_table' )->groupBy('age' )->orderBy('created_at' , 'ASC' );
->groupBy(array ('mytable.myfield1' , 'mytable.myfield2' , 'another_table.myfield3' ));
->orderBy(array ('mytable.myfield1' , 'mytable.myfield2' , 'another_table.myfield3' ));
->having('total_count' , '>' , 2 )
->orHaving('type' , '=' , 'admin' );
->limit(30 );
->offset(10 );
QB::table('my_table' )
->join('another_table' , 'another_table.person_id' , '=' , 'my_table.id' )
->join('another_table' , 'another_table.person_id' , '=' , 'my_table.id' , 'FULL OUTER' )
->join('another_table' , function ($table)
{
$table->on('another_table.person_id' , '=' , 'my_table.id' );
$table->on('another_table.person_id2' , '=' , 'my_table.id2' );
$table->orOn('another_table.age' , '>' , QB::raw(1 ));
})
$query = QB::query('select * from cb_my_table where age = 12' );
var_dump($query->get());
QB::query('select * from cb_my_table where age = ? and name = ?' , array (10 , 'usman' ));
QB::table('my_table' )
->select(QB::raw('count(cb_my_table.id) as tot' ))
->where('value' , '=' , 'Ifrah' )
->where(QB::raw('DATE(?)' , 'now' ))
$data = array (
'name' => 'Sana' ,
'description' => 'Blah'
);
$insertId = QB::table('my_table' )->insert($data);
$data = array (
array (
'name' => 'Sana' ,
'description' => 'Blah'
),
array (
'name' => 'Usman' ,
'description' => 'Blah'
),
);
$insertIds = QB::table('my_table' )->insert($data);
$data = array (
'name' => 'Sana' ,
'counter' => 1
);
$dataUpdate = array (
'name' => 'Sana' ,
'counter' => 2
);
$insertId = QB::table('my_table' )->onDuplicateKeyUpdate($dataUpdate)->insert($data);
$data = array (
'name' => 'Sana' ,
'description' => 'Blah'
);
QB::table('my_table' )->where('id' , 5 )->update($data);
QB::table('my_table' )->where('id' , '>' , 5 )->delete();
QB::transaction(function ($qb) {
$qb->table('my_table' )->insert(array (
'name' => 'Test' ,
'url' => 'example.com'
));
$qb->table('my_table' )->insert(array (
'name' => 'Test2' ,
'url' => 'example.com'
));
});
QB::transaction(function ($qb) {
$qb->table('my_table' )->insert(array ());
$qb->commit();
$qb->rollback();
});
$query = QB::table('my_table' )->where('id' , '=' , 3 );
$queryObj = $query->getQuery();
$queryObj->getSql();
$queryObj->getBindings();
$queryObj->getRawSql();
$subQuery = QB::table('person_details' )->select('details' )->where('person_id' , '=' , 3 );
$query = QB::table('my_table' )
->select('my_table.*' )
->select(QB::subQuery($subQuery, 'table_alias1' ));
$nestedQuery = QB::table(QB::subQuery($query, 'table_alias2' ))->select('*' );
$nestedQuery->get();
QB::pdo();
QB::table('my_table' )->asObject('SomeClass' , array ('ctor' , 'args' ))->first();
QB::table('my_table' )->setFetchMode(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE)->get();
QB::registerEvent('before-select' , 'users' , function ($qb)
{
$qb->where('status' , '!=' , 'banned' );
});
QB::registerEvent('after-insert' , 'my_table' , function ($queryBuilder, $insertId)
{
$data = array ('person_id' => $insertId, 'details' => 'Meh' , 'age' => 5 );
$queryBuilder->table('person_details' )->insert($data);
});
QB::registerEvent('after-insert' , 'person_details' , function ($queryBuilder, $insertId)
{
$queryBuilder->table('person_details' )->where('id' , $insertId)->update(array ('created_at' => date('Y-m-d H:i:s' )));
});
QB::registerEvent('after-delete' , 'my_table' , function ($queryBuilder, $queryObject)
{
$bindings = $queryObject->getBindings();
$queryBuilder->table('person_details' )->where('person_id' , $binding[0 ])->delete();
});
QB::removeEvent('event-name' , 'table-name' );