PHP code example of cradlephp / storm

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

    

cradlephp / storm example snippets


$resource = new PDO('mysql:host=127.0.0.1;dbname=test_db', 'root', '');
$database = new Cradle\Storm\SqlFactory($resource);

$database->query('SELECT * FROM user');  // returns results of raw queries

$query  = 'SELECT * FROM user WHERE user_name LIKE :user_name AND user_active = :user_active';
$bind   = array(':user_name' => '%'.$userName.'%', ':user_active' => 1);

$database->query($query, $bind); // returns results of raw queries

$settings = [
    'user_name'     => 'Chris'
    'user_email'    => '[email protected]'
];

$filter[] = ['user_id=%s', 1];     

// inserts row into 'user' table
$database->insertRow('user', $settings);
// updates rows in 'user' table where user_id is
$database->updateRows('user', $settings, $filter);
// delete rows in 'user' table where user_id is 1
$database->deleteRows('user', $filter);      

$settings = ['user_name' => 'Chris', 'user_email' => '[email protected]'];
// insert row into 'user' table
$database->insertRow('user', $settings);

$settings = [];
$settings[] = ['user_name' => 'Chris', 'user_email' => '[email protected]'];
$settings[] = ['user_name' => 'Dan', 'user_email' => '[email protected]'];
$settings[] = ['user_name' => 'Clark', 'user_email' => '[email protected]'];
// insert multiple rows into 'user' table
$database->insertRows('user', $settings);

$settings = ['user_name' => 'Chris', 'user_email' => '[email protected]'];
$filter[] = ['user_id=%s', 1];
// update row into 'user' table
$database->updateRows('user', $settings, $filter);

$settings = ['user_name' => 'Chris2', 'user_email' => '[email protected]'];
$database->setRow('user', 'user_email', '[email protected]', $settings);

$filter[] = ['user_id=%s', 1];
// delete rows in 'user' table where user_id is 1
$database->deleteRows('user', $filter);

$database
    ->search('user')
    ->setColumns('*')
    ->innerJoinOn('group', 'group_owner=user_id')
    ->leftJoinUsing('friends', 'user_id')
    ->filterByUserName('Chris')
    ->addFilter("user_last LIKE '%s%%'", 'Brown')
    ->sortByUserId('ASC')
    ->addSort('user_last', 'DESC')
    ->setRange(25)
    ->setStart(75)
    ->getRows();

innerJoinOn()
innerJoinUsing()
leftJoinOn()
leftJoinUsing()
rightJoinOn()
rightJoinUsing()
outerJoinOn()
outerJoinUsing()

$database->setRange(75);
$database->setStart(25);
$database->setPage(1);

$database->->setTable('user');
$database->groupBy('user_active');

$database->getTotal();
$database->getRows();
$database->getCollection();

//set user name for all rows
$collection->setUserName('Chris');

// set or get any abstract key for all rows
$collection->setAnyThing()

//collections are iterable
foreach($collection as $model) {        
    echo $model->getUserName().' ';
    echo $model['user_email'];
}

//access as array
echo $collection[0]['user_name'];
//set as array
$collection[0]['user_email'] = '[email protected]';

//save to 'user' table in database
//only relevant columns will be saved
//for all rows
$collection->save('user', $database);

//formats a date column
$collection->formatTime('post_created', 'F d, y g:ia');

//for each row, copy the value of post_user to the user_id column
$collection->copy('post_user', 'user_id');

//remove the row with the index of 1, reindexes all the rows
$collection->cut(1);

//returns the number of rows
$collection->count();

//adds a new row
$collection->add(['post_title' => 'Hi']);

//returns a table array (no objects)
$collection->get();                                      

$model->setUserName('Chris'); //set user name
$model->getUserEmail(); // returns user email

//$model->setAnyThing() // set or get any abstract key

echo $model['user_name']; //access as array
$model['user_email'] = '[email protected]'; //set as array

echo $model->user_name; //access as object
$model->user_name = '[email protected]'; //set as object

//save to 'user' table in database
//only relevant columns will be saved
$model->save('user', $database);

$row = [
    'user_id'       => 1,
    'user_name'     => 'Chris',
    'user_email'    => '[email protected]',
    'post_user'     => 1,
    'post_title'    => 'My Post',
    'post_detail'   => 'This is my new article'
];

$database->model($row)->save('user')->save('post');

//load database
$database
    //search user table
    ->search('user')
    //WHERE user_gender = $_get['gender']
    ->filterByUserGender($_GET['gender'])
    //ORDER BY user_id
    ->sortByUserId('ASC')
    //LIMIT 75, 25
    ->setStart(75)->setRange(25)
    //get a collection object
    ->getCollection()
    //sets all users to active
    ->setUserActive(1)
    //Set a new column post_title
    ->setPostTitle('A '.$_GET['gender'].'\'s Post')
    //Set a new column post_detail
    ->setPostDetail('Content is King')
    //Copy the contents of user_id to a new column post_user
    ->copy('user_id', 'post_user')
    //Set a new column post_created
    ->setPostCreated(time())
    ->formatTime('post_created', 'Y-m-d H:i:s')
    //save to user table
    ->save('user')
    //save to post table
    ->save('post');