PHP code example of kris-ro / php-database-model

1. Go to this page and download the library: Download kris-ro/php-database-model 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/ */

    

kris-ro / php-database-model example snippets




use KrisRo\PhpDatabaseModel\Model;

# Ge the model instance
$model = new Model([
  'host' => 'localhost',
  'database' => 'db_name',
  'username' => 'db_user',
  'password' => 'db_password',
]);

# get the result set as a list of associative array
$rows = $model->getAssocUsersByCondition([
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# fetching one by one
$query = $model->getAssocUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
]);
$oneRow = $query->next();
$oneRow = $query->next();
$oneRow = $query->next();
$oneRow = $query->next();

# get the result set as a list of stdClass objects
$rows = $model->getObjectsUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# fetch the result set indexed by table's id or another unique column
# first column (`salt` in this case) is used for indexing the result set
$rows = $model->getUniqueUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# get rows with key => values pairs
$rows = $model->getIndexedUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# get one column indexed numerically
$rows = $model->getColumnUsersByCondition([
  'select' => 'user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
])->all();

# group and index the result set by the first column specified in the query (`user_profiles_id` in this case)
$rows = $model->getGroupedUsersByCondition([
  'select' => 'user_profiles_id, user_name, email',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 100]
])->all();

# get users with email equal to this value
$model->getAssocUsersByEmail('[email protected]')->all();

# get users with email containing this value (like %%)
$model->getAssocUsersLikeEmail('some-mailbox')->all();

# get users as stdClass objects with email containing this value (like %%)
$rows = $model->getObjectsUsersLikeEmail('kris_ro')->all();

# this will get you the whole table
$rows = $model->getallUsers();

# update values
$updateCount = $model->updateUsersByCondition([
  'condition' => '`users_id` = :id',
  'params' => [':id' => 60],
  'values' => [
    'email' => '[email protected]',
  ],
]);

# update a row by its primary key determined as {table_name}_id
$updated = $model->updateUsers([
  'users_id' => 1200,
  'email' => '[email protected]',
]);

# delete users
$deleted = $model->deleteUsersByCondition([
  'condition' => '`users_id` = :id',
  'params' => [':id' => 101],
]);

# delete row by its primary key determined as {table_name}_id
$deleted = $model->deleteUsers(102);

# counting 
$count = $model->countUsersByCondition([
  'select' => 'salt, user_name',
  'condition' => '`users_id` < :id',
  'params' => [':id' => 20]
]);

# adding new records
$count = $model->setUsers([
  'user_profiles_id' => '4', 
  'salt' => 'ecvfr56765ty', 
  'user_name' => 'Test Test', 
  'email' => '[email protected]',
]);

# save the record and return the last inserted ID
$id = $model->setUsersAndGetId([
  'user_profiles_id' => '4', 
  'salt' => 'cx6uykuy', 
  'user_name' => 'Another Test', 
  'email' => '[email protected]',
]);

# batch insert; returns the number of rows inserted
$count = $model->setUsers([
  [
    'user_profiles_id' => '4', 
    'salt' => 'ecvfr56765ty', 
    'user_name' => 'Test Test', 
    'email' => bin2hex(random_bytes(8)) . '[email protected]',
  ],
  [
    'user_profiles_id' => '4', 
    'salt' => 'cx6uykuy', 
    'user_name' => 'Another Test', 
    'email' => bin2hex(random_bytes(8)) . '[email protected]',
  ]
]);