PHPackages                             cry/cry-cms-db - 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. cry/cry-cms-db

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

cry/cry-cms-db
==============

PHP Class for working with MySQL via PDO

1.12(11mo ago)055↓50%2MITPHP

Since Aug 13Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/CryInt/CryCMS-Db)[ Packagist](https://packagist.org/packages/cry/cry-cms-db)[ RSS](/packages/cry-cry-cms-db/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)DependenciesVersions (14)Used By (2)

CryCMS-Db
=========

[](#crycms-db)

PHP Class for working with MYSQL via PDO.

Write queries or use methods.

Singleton.

`Setup`

```
Db::config([
    'host' => 'localhost',
    'user' => 'test',
    'password' => 'test',
    'database' => 'test',
]);
```

`Debug mode`

```
Db::debug(true);
```

---

### Two ways of queries

[](#two-ways-of-queries)

`Native SQL query (also with placeholders)`

```
Db::sql()->query('query', [])->exec();
Db::sql()->query('query', [])->getOne();
Db::sql()->query('query', [])->getAll();
```

`Via builder`

```
$result = Db::table('table')->getOne();
$result = Db::table('table')->getAll();
```

---

### Examples

[](#examples)

`Create table`

```
Db::table('table')->create([
    'id' => 'INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
    'name' => 'VARCHAR(255)',
    'date' => 'DATETIME',
], 'ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COMMENT="TEST"');
```

`Add index`

```
Db::table('table')->index(['id', 'date'], 'UNIQUE');
```

`Truncate table`

```
Db::table('table')->truncate();
```

`Drop table`

```
Db::table('table')->drop();
```

---

`Get list of table fields`

```
Db::table('table')->fields();
```

`Queries log (debug=true only)`

```
$log = Db::getLog();
```

`Get query instead of execute`

```
replace methods getOne() or getAll() to getSQL()

```

---

`Insert`

```
Db::table('table')->insert([
    'name' => 'first',
    'date' => date('Y-m-d'),
]);
```

`Get autoincrement after insert`

```
$id = Db::lastInsertId();
```

`Get one row - case 1`

```
$one = Db::table('table')
    ->select(['id', 'name', 'date'])
    ->where(['id = :id'])
    ->values(['id' => $id])
    ->getOne();
```

`Get one row - case 2`

```
$one = Db::table('table')
    ->select(['id', 'name', 'date'])
    ->where(['id' => 1])
    ->getOne();
```

`Get list of rows`

```
$all = Db::table('table', 't')
    ->select(['t.id', 'td.field_1', 'td.field_2'])
    ->calcRows()
    ->leftJoin('testData', 'td', 'td.test_id = t.id')
    ->where(['t.date
