PHPackages                             mfurman/pdomodel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mfurman/pdomodel

ActiveLibrary[Database &amp; ORM](/categories/database)

mfurman/pdomodel
================

It is a library of PDO model representations

1.0.3(2y ago)010MITPHP

Since Jul 20Pushed 2y ago1 watchersCompare

[ Source](https://github.com/MichalFurman/PDOModel)[ Packagist](https://packagist.org/packages/mfurman/pdomodel)[ RSS](/packages/mfurman-pdomodel/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (5)Used By (0)

PDO Model
=========

[](#pdo-model)

PDO access class with PDO model - version 1.0.3

Changes from version 1.0.3
--------------------------

[](#changes-from-version-103)

- minor corrections in code to version 1.0.2,
- minor corrections in interfaces for exists classes,
- added simply logger method,
- added check for PDO singleton instance,
- alignment for variable names,

Changes from version 1.0.2
--------------------------

[](#changes-from-version-102)

- changed database table declaration in model - now You have to declare table in constructor as a parent property after PDOAccess instance,
- changed execution protected methods in model - now You don't have to set name of table as parameter in execute method - table is declared by constructor,
- added two public methods - "set\_table" and "get\_table" - methods are setter and getter for change declared name of table in constructor during model life,
- changed way of execution public method "exist" - now You can set a table for this method as the last calling parameter, if You don't declare this parameter method will execute on exist declared table by constructor,

Simple Instalation
------------------

[](#simple-instalation)

Just use composer for install new dependency: `composer require mfurman/pdomodel "1.0.3"`

Simple Usage
------------

[](#simple-usage)

First set config for MySQL database (it can be simple array):

```
  $config = [
    'DB_CONNECTION'  => 'mysql',
    'DB_HOST'        => 'localhost',
    'DB_PORT'        => '3306',
    'DB_DATABASE'    => 'testdb',
    'DB_USERNAME'    => 'access',
    'DB_PASSWORD'    => 'password',
    'LOGGER_DIR'     =>  'logger directory started from base directory of project (default "./errors")',
  ];
```

Then initialize singletone instance of PDOAccess by execute static method GET:

```
   \mfurman\pdomodel\PDOAccess::get($config);
```

At this moment You have one instance of PDO Access class in Your app, You can get this instance whenever You want.

Now You can create simply model by extending dataDb class (this class has state of specific model):

#### Model example:

[](#model-example)

```
    use \mfurman\pdomodel\PDOAccess;
    use \mfurman\pdomodel\dataDb;

    class User extends dataDb
    {
        private $users_table = 'users';

        public function __construct($commit=true)
        {
            parent::__construct(PDOAccess::get(), $this->users_table, $commit, false);
        }

        // return associative array, table of users
        public function getAll() :array
        {
            return $this->read('*')->get();
        }

        // return associative array, users data
        public function getById(int $id) :array
        {
            return $this->read('*','id = '.$id)->get();
        }

        // return associative array, users data
        public function getByName(string $name)
        {
            return $this->read('*','user_name = "'.$name.'"')->get();
        }

        // return id of new row,
        public function add(array $data) :int
        {
            $this->set(array('user_name'=>$data['user_name']));
            return $this->insert();
        }

        // return true or false
        public function updateOne(array $data, int $id) :bool
        {
            if (empty($this->read('*','id = '.$id)->get())) return false;
            $this->set(array('user_name'=>$data['user_name']));
            $this->update($id);
            return true;
        }

        public function deleteById(int $id) :bool
        {
            if (empty($this->read('*','id = '.$id)->get())) return false;
            $this->delete_where('id = '.$id);
            return true;
        }
    }
```

When You created models, You can create some repositories. In repositories You have control at database transactions and commits.

#### Repository example:

[](#repository-example)

```
    use Myvendor\Myapp\Models\Task;
    use Myvendor\Myapp\Models\User;

    class TaskRepository
    {
        private $task;
        private $user;

        (...)

        public function store(array $data) :array
        {
            $this->task = new Task(false);                      // - set autocommit to false (true/false)
            $this->user = new User(false);                      // - set autocommit to false (true/false)

            // inform PDO about start transaction
            $this->task->begin();

            $this->user->getByName($data['user_name']);
            if ($this->user->is()) $data['user_id'] = $this->user->get('id');
            else {
                $this->user->reset();
                $data['user_id'] = $this->user->add($data);
            }

            $id = $this->task->add($data);

            // inform PDO to commit ealier started transaction, if something go wrong - commit will rollback
            $this->task->commit();

            // this toggle change model to autocommit transactions (true/false)
            $this->task->set_commit(true);
            return $this->task->getById($id);
        }
```

Class dataDb has builded methods, which we can use in our model or repository.
------------------------------------------------------------------------------

[](#class-datadb-has-builded-methods-which-we-can-use-in-our-model-or-repository)

### Public methods (can be use in model and repository):

[](#public-methods-can-be-use-in-model-and-repository)

Constructor - parametres: (PDOModel-singletone, database\_table, commit, flag to register any saves to database in log table)

```
    parent::__construct(object $dbaccess, $this->myTable, $commit=true, $log_rec=false);
```

If You would like to register activity in log table You have to create 'general\_log' table (look at the end). If not, just set this toggle to 'false' or do't use it - it's false by default

set\_commit - toggle to change auto commit - default by 'true'

```
    $this->set_commit(bool $flag=true);
```

reset - reset state of model, it doesn't reset table name

```
    $this->reset();
```

set\_table - change or set database table for model (new method)

```
    $this->set_table(string $table);
```

get\_table - return actual database table name set in model (new method)

```
    $this->get_table();
```

is - inform about state - is hydrated or not - return 'true/false'

```
    $this->is();
```

set - set data to model

```
    $this->set('name','value');

    //or we can set multiple
    $this->set(array(
        'name01' => 'value01'
        'name02' => 'value02'
        'name03' => 'value03'
        )
    );
```

get - get value by name from model

```
    $this->get('name');

    //or if is multiple data table -  for eg. table of 'users'
    $this->get('name',0); // - get name of first user
```

get\_flat - get array of specific data as one dimension array

```
    $this->get_flat();
```

del - remove some value from state by name

```
    $this->del('name');
```

begin - start transaction when autocommit is false

```
    $this->begin();
```

commit - commit started transaction when autocommit is false

```
    $this->commit();
```

### Protected methods (can be use in model only):

[](#protected-methods-can-be-use-in-model-only)

read - read data from table, hydrate state of model - execute (commit) - return model object, can be chaining

$this-&gt;read(table, columns, where, order, limit) :object

```
    $this->read('*', 'name = "John"') :object

    // or
    $this->read(['ID', 'name', 'surname', 'city'], 'name = "John" AND age > 23', 'DESC', 1) :object
```

insert - insert data to database table - execute (commit) - return new ID of created row

```
    $this->insert() :int
```

update - update data to existing row in table - execute (commit) - return model object, can be chaining

```
    $this->update($id) :object
```

update\_where - update data to existing row in table - execute (commit) - return model object, can be chaining

```
    $this->update_where('name = "John"') :object
```

delete\_where - delete data from table - execute (commit) - return model object, can be chaining

```
    $this->delete_where('name = "John"') :object
```

exists - check if data exists in database - return true/false

$this-&gt;exists(table,\['name1' =&gt;'value1', 'name2' =&gt; 'value2', ...\], where(in SQL), table (optional))

```
    $this->exists(['name' => 'John', 'surname' => 'Smith']) :bool

    //or
    $this->exists(['name' => 'John', 'surname' => 'Smith'], 'ID != '.$id, $this->otherTable) :bool
```

General log table structures
----------------------------

[](#general-log-table-structures)

General log table is used when we have set session between user and server. It using session data to recognize existing user and his activity. We have to set two session data for logged user:

```
    $_SESSION['user_id'] = 12               // set when user log in - for eg ID from users table,
    $_SESSION['user_name'] = 'John Smith'   // set when user log in - username from users table,
```

General log table must have these specyfic columns and name:

table name: 'general\_log'

columns:

- 'date': timestampler,
- 'user\_id': int - $\_SESSION\['user\_id'\]
- 'user\_name': string - $\_SESSION\['user\_name'\]
- 'method': string - (INSERT, UPDATE, DELETE - methods that executed on database)
- 'value': string - (SQL query that the user generated)

All data in general log will be automatically filled when flag 'log\_rec' is set to 'true'.

Enjoy.

License 🗝️
----------

[](#license-old_key)

Under license (MIT, Apache etc)

MIT © Michał Furman

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~232 days

Total

4

Last Release

1056d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3fdbfa8d30c62b56338b4677b4ce0ec6873b13e95b44c230495ba7a8a267ccef?d=identicon)[MichalFurman](/maintainers/MichalFurman)

---

Top Contributors

[![MichalFurman](https://avatars.githubusercontent.com/u/64414824?v=4)](https://github.com/MichalFurman "MichalFurman (47 commits)")

---

Tags

phpmysqlsqlpdomodel

### Embed Badge

![Health badge](/badges/mfurman-pdomodel/health.svg)

```
[![Health](https://phpackages.com/badges/mfurman-pdomodel/health.svg)](https://phpackages.com/packages/mfurman-pdomodel)
```

###  Alternatives

[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k5.5M69](/packages/ifsnop-mysqldump-php)[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k22.9k](/packages/clouddueling-mysqldump-php)[druidfi/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

35489.8k6](/packages/druidfi-mysqldump-php)[eftec/pdoone

Minimaist procedural PDO wrapper library

1105.9k9](/packages/eftec-pdoone)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
