PHP Classes

How to a Create PHP Model Class that Stores Objects in a Database Using the Package PHP Database Model: Access model objects stored in a database

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-09-04 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 48 This week: 2All time: 10,739 This week: 16Up
Version License PHP version Categories
php-database-model 1.0MIT/X Consortium ...8Databases, Design Patterns, PHP 8
Description 

Author

This package can access model objects stored in a database.

It provides a base class that performs common database queries using PDO.

The package also provides a sub-class that performs operations on model objects stored in database tables like:

- Get individual model objects of a class

- Get all model objects of a class

- Set properties with values for a given model object

- Get the values of properties of a given model object

- Delete given model objects

- Etc...

Picture of Cristian Radu
Name: Cristian Radu <contact>
Classes: 4 packages by
Country: Romania Romania
Age: ???
All time rank: 438580 in Romania Romania
Week rank: 9 Up1 in Romania Romania Up

Documentation

PHP Database Modeling

The scope of this class is to streamline the development. If you need a well structured library or system like ORM or Active Record then this is not for you. My aim here was to have a way to make an easy semantic request and get a result set from database. PDO is working behind the scene.

Installation

Use composer to install PHP Database Modeling.

composer require kris-ro/php-database-model

Usage

These are all magic calls (processed with __call()) in the form:\ get(Assoc|Objects|Unique|Column|Indexed|Grouped){Table_name}ByCondition()->(all|next)()\ get(Assoc|Objects|Unique|Column|Indexed|Grouped){Table_name}By{Column_name}()->(all|next)()\ set{Table_name}()\ set{Table_name}AndGetId()\ update{Table_name}ByCondition()\ update{Table_name}()\ delete{Table_name}ByCondition()\ delete{Table_name}()\ count{Table_name}ByCondition()\

require YOUR_PATH . '/vendor/autoload.php';

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('some-mailbox@some-non-domain.con')->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' => 'tralala@some-domain.toto',
  ],
]);

# update a row by its primary key determined as {table_name}_id
$updated = $model->updateUsers([
  'users_id' => 1200,
  'email' => 'tralala-again@some-domain.toto',
]);

# 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' => 'test.test@some-domain.toto',
]);

# save the record and return the last inserted ID
$id = $model->setUsersAndGetId([
  'user_profiles_id' => '4', 
  'salt' => 'cx6uykuy', 
  'user_name' => 'Another Test', 
  'email' => 'another.test@some-domain.toto',
]);

# 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)) . '-testy.test@some-domain.toto',
  ],
  [
    'user_profiles_id' => '4', 
    'salt' => 'cx6uykuy', 
    'user_name' => 'Another Test', 
    'email' => bin2hex(random_bytes(8)) . '-another.testy@some-domain.toto',
  ]
]);

License

MIT


  Files folder image Files (7)  
File Role Description
Files folder imagesrc (2 files)
Files folder imagetests (3 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file Database.php Class Class source
  Plain text file Model.php Class Class source

  Files folder image Files (7)  /  tests  
File Role Description
  Plain text file DatabaseTest.php Class Class source
  Plain text file ModelTest.php Class Class source
  Accessible without login Plain text file test.sql Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:48
This week:2
All time:10,739
This week:16Up