PHPackages                             nulpunkt/yesql-php - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. nulpunkt/yesql-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nulpunkt/yesql-php
==================

A clone of the wonderful yesql clojure library, for php

v1.5.1(1y ago)2512.5k7[1 issues](https://github.com/nulpunkt/yesql-php/issues)[1 PRs](https://github.com/nulpunkt/yesql-php/pulls)MITPHP

Since Mar 18Pushed 1y ago6 watchersCompare

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

READMEChangelog (9)Dependencies (2)Versions (10)Used By (0)

yesql-php
=========

[](#yesql-php)

This is a clone of the wonderful [yesql library from clojure](https://github.com/krisajenkins/yesql). The idea is to have a seperate sql file for queries, which you can then access as methods on a class.

I built for fun on a Friday, liked it a lot and now we are using it where I work. So I guess it's production ready.

Installation
------------

[](#installation)

Use composer to require:

```
"nulpunkt/yesql-php": "^1"

```

Examples!
=========

[](#examples)

You need to make a repository of queries:

```
$pdo = new PDO($host, $user, $pass); // Fill in the blanks
$r = new Nulpunkt\Yesql\Repository($pdo, "my-queries/queries.sql");
```

Fetching many rows
------------------

[](#fetching-many-rows)

in `queries.sql` we can put:

```
-- name: getAllRows
-- This will fetch all rows from test_table
select * from test_table;
```

which will allow us to call

```
$r->getAllRows();
```

Inserting a row
---------------

[](#inserting-a-row)

A database without rows is not of much use, lets insert some data:

```
-- name: insertRow(thing)
insert into test_table (something) values (:thing)
```

```
// returns the insertId
$r->insertRow('a thing');
```

As default, yesql will simply bind all params passed to the called function, to the query associated with it. We'll see how to make mappers further down.

Updating a row
--------------

[](#updating-a-row)

Maybe we need to fix some exsisting data

```
-- name: updateRow(id, thing)
update test_table set something = :thing where id = :id
```

```
// returns the number of rows touched by the update
$r->updateRow(3, 'fixed thing');
```

Fetching a single row
---------------------

[](#fetching-a-single-row)

yesql-php support different modlines, lets say we know we only need to get one row:

```
-- name: getById(id) oneOrMany: one
select * from test_table where id = :id;
```

```
// Fetches one row with id 3
$r->getById(3);
```

Fetching and mapping rows in one go
-----------------------------------

[](#fetching-and-mapping-rows-in-one-go)

Maybe we want to return a modified version of the row. By specifying a rowFunc, we can have a function called, on every row returned:

```
-- name: getMappedById(id) oneOrMany: one rowFunc: MyObject::mapRow
select * from test_table where id = :id
```

```
class MyObject {
  public static function mapRow($r) {
    return ['id' => $r['id'], 'ohwow' => $r['something']];
  }
}
// return one row, with keys id and ohwow
$r->getMappedById(3);
```

Mapping rows to objects
-----------------------

[](#mapping-rows-to-objects)

Sometimes an object is want you want, rowClass got your back:

```
-- name: getObjectById(id) oneOrMany: one rowClass: MyObject
select * from test_table where id = :id
```

```
class MyObject {
}
// return one row, which is an instance of MyObject with id and something set
$r->getObjectById(3);
```

Mapping data on the way in
--------------------------

[](#mapping-data-on-the-way-in)

We may need to map our domain objects to be able to insert them into the database.

```
-- name: insertObject inFunc: MyObject::toRow
insert into test_table (id, something) values (:id, :something)
```

```
class MyObject {
  // $i will be the arguments passed to insertObject
  public static function toRow($i, $o) {
    return ['id' => $i, 'something' => $o->something];
  }
}
$o = new MyObject;
$r->insertObject($i, $o)
```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 98.2% 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 ~389 days

Recently: every ~771 days

Total

9

Last Release

598d ago

### Community

Maintainers

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

---

Top Contributors

[![nulpunkt](https://avatars.githubusercontent.com/u/1875948?v=4)](https://github.com/nulpunkt "nulpunkt (54 commits)")[![terales](https://avatars.githubusercontent.com/u/1920639?v=4)](https://github.com/terales "terales (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/nulpunkt-yesql-php/health.svg)

```
[![Health](https://phpackages.com/badges/nulpunkt-yesql-php/health.svg)](https://phpackages.com/packages/nulpunkt-yesql-php)
```

PHPackages © 2026

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