PHPackages                             niikunihiro/poulsen - 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. niikunihiro/poulsen

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

niikunihiro/poulsen
===================

Simple Query Builder for PHP

18[2 issues](https://github.com/niikunihiro/poulsen/issues)PHP

Since Jun 14Pushed 10y ago1 watchersCompare

[ Source](https://github.com/niikunihiro/poulsen)[ Packagist](https://packagist.org/packages/niikunihiro/poulsen)[ RSS](/packages/niikunihiro-poulsen/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Poulsen Simple Query Builder [![Build Status](https://camo.githubusercontent.com/ff044bab3618e67becb1921c495648d7a0fb7b80b92c79b849d0c6ec78025509/68747470733a2f2f7472617669732d63692e6f72672f6e69696b756e696869726f2f706f756c73656e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/niikunihiro/poulsen)
=====================================================================================================================================================================================================================================================================================================================

[](#poulsen-simple-query-builder-)

PHP 5.3 で動くクエリービルダー

要件
--

[](#要件)

- PHP5.3以上

サポート
----

[](#サポート)

- MySQL

インストール
------

[](#インストール)

Composer経由でインストールします

```
{
    "require": "niikunihiro/poulsen": "dev-master"
}
```

使い方
---

[](#使い方)

src/Config/database.php でアクセス情報を設定する

```
return array(
    'default' => 'mysql',
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database',
            'username'  => 'username',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ),
    ),
);
```

### 基本

[](#基本)

Builderクラスのインスタンスを生成し、table()メソッドで対象のテーブルを指定します。

```
require_once __DIR__ .'/vendor/autoload.php';
use Poulsen\Query\Builder;

$DB = new Builder;
$query = $DB->table('users');
```

または、インスタンスの生成時にテーブルを指定する事も出来ます

```
$query = new Builder('users');
```

テーブルを指定したBuilderクラスのインスタンスを取得したら、続けてデータの取得、挿入、更新、削除用のメソッドを指定して操作を行います。 Builderクラスのメソッドは直感的に扱えるようにチェーンメソッドで呼び出せるようになっています。

### データの取得

[](#データの取得)

#### データを配列で取得

[](#データを配列で取得)

配列の各要素はオブジェクトになっています。

```
$users = $DB->table('users')->get();
```

#### データを一件取得

[](#データを一件取得)

一件だけ取得したデータはオブジェクトになっています。

```
$user = $DB->table('users')->first();
```

#### カラムを指定して、データを取得

[](#カラムを指定してデータを取得)

```
$users = $DB->table('users')->select('id', 'name', 'email AS mail_address')->first();
```

#### 条件を指定する

[](#条件を指定する)

```
$result = $DB->table('users')->where('name', '=', 'niikunihiro')->get();
```

#### LIKE条件を指定する

[](#like条件を指定する)

```
$users = $DB->table('users')->where('name', 'LIKE', 'nii%')->get();
```

#### 複数条件を指定する

[](#複数条件を指定する)

```
$users = $DB->table('users')
    ->where('name', 'LIKE', 'nii%')
    ->orWhere('name', '', 'poulsen')
    ->get();
```

#### もうちょっと複雑な条件を指定する

[](#もうちょっと複雑な条件を指定する)

`where()`メソッドの引数に無名関数を渡すと括弧で囲んだ条件を作成する事が出来ます

```
$user = $DB->table('users')
    ->where('name', '=', 'poulsen')
    ->orWhere(function($query)
    {
        $query->whereIn('role_id', array(1, 2, 3));
        $query->where('updated_at', '>', '2015-05-08 11:00:00');
    })
    ->get();
```

上の例は次のSQLをビルドします

```
SELECT * FROM users
WHERE 1 AND name = 'poulsen'
OR (role_id IN(1, 2, 3) AND updated_at > '2015-05-08 11:00:00')
```

#### テーブル結合

[](#テーブル結合)

テーブル結合を行う場合は`join()`メソッドを使用します。
`join()`メソッドは第1引数に結合するテーブル名を指定します。第2引数から第4引数まではON句の条件を指定します。

```
$comments = $DB->table('articles')
            ->select('articles.title', 'comments.body AS comment')
            ->join('comments', 'article_id', '=', 'id')
            ->get();
```

外部結合を使いたい場合は`join()`メソッドの第5引数にLEFTまたはRIGHTを指定してください。

```
$comments = $DB->table('articles')
            ->select('articles.title', 'comments.body AS comment')
            ->join('comments', 'article_id', '=', 'id', 'RIGHT')
            ->get();
```

### レコード挿入

[](#レコード挿入)

`values()`メソッドでデータを設定し、`insert()`メソッドでレコードを挿入します。
`insert()`メソッドは挿入したレコードのIDを返します

```
$now = with(new DateTime)->format('Y-m-d H:i:s');
$id = $DB->table('users')
         ->values('username', 'poulsen')
         ->values('email', 'poulsen@example.com')
         ->values('password', '$2y$10$/L1ScTA7H7KTfS0Josftk.qPeAygzOZOB1GClEkTlMzmmuFw7/Yxa')
         ->values('created_at', $now)
         ->values('updated_at', $now)
         ->insert();
```

> `with()`関数はコンストラクターメソッドチェーンのためのショートカットに利用できます。

### レコード更新

[](#レコード更新)

`set()`メソッドでデータを設定し、`update()`メソッドでレコードを更新します。

```
$now = with(new DateTime)->format('Y-m-d H:i:s');
$DB->table('users')
         ->set('rank', 3)
         ->set('updated_at', $now)
         ->update();
```

条件を指定する場合は、`where()`メソッド等を利用します。

```
$DB->table('users')
         ->set('rank', 1)
         ->set('updated_at', $now)
         ->where('id', '=', 32)
         ->update();
```

### レコード削除

[](#レコード削除)

レコードを削除する場合は、`delete()`メソッドで削除します。
※`where()`メソッド等で条件を指定しない場合は、削除を行わずに`false`を返します。

```
$DB->table('users')
         ->where('id', '=', 32)
         ->delete();
```

### クエリーログ

[](#クエリーログ)

クエリーログを確認するために、[ChromePhp](https://github.com/ccampbell/chromephp)を内部で使用しています。

ログの出力を停止する場合は、`Poulsen\Query\Builder`クラスのオブジェクト定数`ENVIRON`の値を`production`に設定します。

```
namespace Poulsen\Query;

class Builder {

    /** 環境 'production'の場合はログを出力しない */
    const ENVIRON = 'develop';
```

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

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/e0bce6c2185f8d60dc3d5b02dd9b3adfd2891d6bb60a71291363f6eac3063d1f?d=identicon)[niikunihiro](/maintainers/niikunihiro)

---

Top Contributors

[![niikunihiro](https://avatars.githubusercontent.com/u/2450056?v=4)](https://github.com/niikunihiro "niikunihiro (20 commits)")

### Embed Badge

![Health badge](/badges/niikunihiro-poulsen/health.svg)

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

###  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.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

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

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

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