PHPackages                             atayahmet/database-active-record-class - 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. atayahmet/database-active-record-class

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

atayahmet/database-active-record-class
======================================

The written for fast, practical database active record actions

2632PHP

Since Nov 25Pushed 11y ago1 watchersCompare

[ Source](https://github.com/atayahmet/database-active-record-class)[ Packagist](https://packagist.org/packages/atayahmet/database-active-record-class)[ RSS](/packages/atayahmet-database-active-record-class/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Database active record
======================

[](#database-active-record)

> **Available fields:**

> - This library completly is written for composer package
> - This will works with vendor autoload
> - Codeigniter active record class interface used
> - These library use simple and fast of

Let's start!
------------

[](#lets-start)

[- Database Configuration](#database-configuration)

[- We first make the database connection](#we-first-make-the-database-connection)

[- SELECT](#select)

[- FROM](#from)

[- WHERE](#where)

[- LIKE COMBINATION](#like-combination)

[- ORDER BY](#order-by)

[- GROUP BY](#group-by)

[- HAVING](#having)

[- LIMIT](#limit)

[- OFFSET (skip data)](#offset-skip-data)

[- JOIN TABLES](#join-tables)

[- INSERT](#insert)

[- UPDATE](#update)

[- DELETE](#delete)

[- COUNT](#count)

[- Native Query](#native-query)

[- GET](#get)

[- DB PREFIX](#db-prefix)

[- Num Rows](#num-rows)

[- Row](#row)

[- Processing the query results](#processing-the-query-results)

[- Affected Rows](#affected-rows)

[- SQL Dump](#sql-dump)

Database Configuration
----------------------

[](#database-configuration)

First let's start with the database settings.

database configuration files in the **Db** folders -&gt; **config.php**

```
$current = 'mysql:connect1';

$db = array(
	'mysql' => array(
		'connect1' => array(
			'hostname' => 'localhost',
			'username' => 'root',
			'password' => '',
			'database' => '',
			'dbprefix' => ''

		)
	)
);
```

The **$current** variable is the driver you want to use as the active and allows you to use the database connection.

**Example:**

Up when I want to define a second database connection settings you need to do the following.

```
	'connect2' => array(
			'hostname' => 'localhost',
			'username' => 'root',
			'password' => '',
			'database' => '',
			'dbprefix' => ''

		)

```

and my **$current** variable have been:

```
$current = 'mysql:connect2';
```

We can define the connection as we want it that way.

> **Note:*****mysql*** needs to be defined for the identification of the considered prospective.

We first make the database connection
-------------------------------------

[](#we-first-make-the-database-connection)

Add our workspace our library

```
use Db\Query as DB;
```

We install the library and also have set a alias. I chose the DB alias.

**A simple database query:**

```
DB::select('*');
DB::get('example_table');
```

We questioned directly above our table without specifying any criteria query. We can do the same query in the following way:

```
DB::select('*')->get('example_table');
```

**SELECT:**
-----------

[](#select)

**Use 1:**

```
DB::select('*')->get('example_table');
```

**Use 2:**

```
DB::select('examle_type.*')->get('example_type');
```

**Use 3:**

```
DB::select('example_type.id');
DB::select('example_type.name')->get('example_type');
```

\*\*select\_max():\*\* ```
$result = DB::select_max('id')->get('example_type');

echo $result->row()->id;
```

\*\*select\_min():\*\* ```
$result = DB::select_max('id')->get('example_type');

echo $result->row()->id;
```

\*\*select\_avg():\*\* ```
$result = DB::select_avg('age')->get('example_type');

echo $result->row()->age;
```

\*\*select\_sum():\*\* ```
$result = DB::select_sum('total')->get('example_type');

echo $result->row()->total;
```

\*\*distinct():\*\* ```
$result = DB::distinct('city')->get('example_type');

echo $result->row()->city;
```

**FROM:**
---------

[](#from)

**from():**

```
$result = DB::select('*')->from('example_table')->get();

echo $result->row()->total;
```

**WHERE**
---------

[](#where)

```
$result = DB::where('city','Istanbul')->get('users');

print_r $result->result_array();
```

Where you can pass parameters to the method in 3 ways.

**Method 1:**

```
$result = DB::where('city !=','Istanbul')->get('users');

print_r $result->result_array();
```

```
$result = DB::where('age >',19)->get('users');

print_r $result->result_array();
```

```
$result = DB::where('age ' => 19))->get('users');

print_r $result->result_array();
```

```
$result = DB::where(array('age ',18)
	->get();
```

We combine the member table where the city table. And we have defined the coming of the age of 18 and where the.

> **Note:**We have sent the left marked as the third parameter in the join method. Parameters that are available here:

- **inner** (INNER JOIN)
- **left** (LEFT JOIN)
- **right** (RIGHT JOIN)
- **left outer join** (LEFT OUTER JOIN)
- **right outer join** (RIGHT OUTER JOIN)
- **cross** (CROSS JOIN)

**inner** parameters will work as default.

Let's make different example:

```
DB::select('t1.name, t2.city')
	->from(DB::dbprefix('users') . ' t1')
	->join(DB::dbprefix('cities') . ' t2',"t2.id = t1.city_id",'inner')
	->join(DB::dbprefix('countries') . ' t3','t3.id = t2.country_id','left')
	->where('t1.age ',18)
	->get();
```

**INSERT**
----------

[](#insert)

There are several ways to add data to the table.

**insert():**

First:

```
DB::insert('users',array(
		'name' => 'Ali',
		'city' => 'Istanbul',
		'age' => 21
	)
)
```

Another use: ```sh DB::set('name','Ali'); DB::set('city','Istanbul'); DB::set('age','18'); DB::insert('users'); ``` ```sh DB::set( array( 'name' =&gt; 'Ali', 'city' =&gt; 'Istanbul/Turkey', 'age' =&gt; 18 ) ); DB::insert('users');

```

and another use than:
```sh
class User {
	public $name = 'Ali';
	public $city = 'Istanbul';
	public $age = 18;
}

DB::insert('users', new User());

```

\*\*insert\_batch():\*\* ```sh DB::insert\_batch('users',array( array( 'name' =&gt; 'Ali', 'city' =&gt; 'Istanbul', 'age' =&gt; 21 ), array( 'name' =&gt; 'Erkan', 'city' =&gt; 'Ankara', 'age' =&gt; 20 ), array( 'name' =&gt; 'Emre', 'city' =&gt; 'Izmir', 'age' =&gt; 19 ) ) ) ```
\*\*insert\_id():\*\* After adding to retrieve the last record id:

```
DB::insert_id();
```

**UPDATE**
----------

[](#update)

Relatively simple processing such as insert, update

**update():**

```
DB::where('id',1)
	->update('users',array(
		'name' => 'Ali',
		'city' => 'Istanbul/Turkey',
		'age' => 18
	)
)
```

or

```
DB::set('name','Ali');
DB::set('city','Istanbul/Turkey');
DB::set('age',18);
DB::update('users');
```

or

```
DB::set(
	array(
		'name' => 'Ali',
		'city' => 'Istanbul/Turkey',
		'age' => 18
	)
);

DB::update('users');
```

\*\*update\_batch():\*\* Sometimes we want to do multiple updates.

```
$data = array(
	array(
		'id' => 1,
		'name' => 'Ali',
		'city' => 'Izmir',
		'age' => 19
	),
	array(
		'id' => 2,
		'name' => 'Ahmet',
		'city' => 'Bursa',
		'age' => 21
	),
	array(
		'id' => 3,
		'name' => 'Adem',
		'city' => 'Antalya',
		'age' => 22
	)
);

DB::update_batch('users',data, 'id');
```

**DELETE**
----------

[](#delete)

**delete():**

```
DB::where('id',1)->delete('users');
```

**COUNT**
---------

[](#count)

Get the number of records in the table are also able to do a fairly simple way.

**count\_all():**

```
DB::count_all('users');
```

This method will return us to the number of records in the specified table

\*\*count\_all\_results():\*\* ```sh DB::from('users') -&gt;where('age &gt;',18) -&gt;or\_where('city','Istanbul') -&gt;count\_all\_results(); ``` &gt; \*\*Note:\*\* as much as possible when you want to use this method of total records Native Query:
-------------

[](#native-query)

if you say you want to run native SQL.

**query():**

```
DB::query("SELECT * FROM users WHERE age > 18");
```

GET:
----

[](#get)

Is a method that will run our query. If you wish you can send your query table names get method. If you wish, you can choose the method from.

**get():**

```
DB::get('users');
```

or

```
DB::from('users')->get();
```

**get\_where():**

```
$limit = 1;
$offset = 2;

DB::get_where('users',array('id' => 1),$limit,$offset);
```

DB PREFIX
---------

[](#db-prefix)

We use our unique method we want to use the prefix table.

```
DB::dbprefix('users');
```

Num Rows
--------

[](#num-rows)

We can use it to get the number of rows of query results.

**num\_rows():**

```
$result = DB::get('users');

echo $result->num_rows();
```

Row
---

[](#row)

Allows access to a single row in the query results.

**row():**

The result will be the object.

```
$result = DB::get('users');

print_r $result->row();
```

or it can be done in specifying the number of rows you want to access

```
print_r $result->row(5);
```

\*\*row\_array():\*\* The result will be the array.

```
$result = DB::get('users');

print_r $result->row_array();
```

or it can be done in specifying the number of rows you want to access

```
print_r $result->row_array(5);
```

Processing the query results
----------------------------

[](#processing-the-query-results)

If we want to use queries in a loop we run it we can do in two ways.

**result():**

```
$result = DB::get('users');

print_r $result->result();
```

> **Note:** results will become an object

\*\*result\_array():\*\* ```
$result = DB::get('users');

print_r $result->result_array();
```

> **Note:** results will become an array

Affected Rows
-------------

[](#affected-rows)

**affected\_rows():**

```
echo DB::affected_rows();
```

SQL Dump
--------

[](#sql-dump)

When running under the URL of a page request is sent to all queries will return the string and working duration.

**dump():**

one will give way listed in a table.

```
echo DB::dump();
```

If we want we can also take in a number of.

```
print_r DB::dump('array');
```

hopefully be helpful to you!

Please errors and parts you do not understand that you can discuss open issues identified under the project.

happy coding!
-------------

[](#happy-coding)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c1b8378df11a8ebd99af14e150aa27201937b97cc08a5f9f85985f7ab62b667f?d=identicon)[atayahmet](/maintainers/atayahmet)

---

Top Contributors

[![atayahmet](https://avatars.githubusercontent.com/u/1698044?v=4)](https://github.com/atayahmet "atayahmet (114 commits)")

### Embed Badge

![Health badge](/badges/atayahmet-database-active-record-class/health.svg)

```
[![Health](https://phpackages.com/badges/atayahmet-database-active-record-class/health.svg)](https://phpackages.com/packages/atayahmet-database-active-record-class)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M542](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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