PHPackages                             marsapp/sqlhelper-yii2 - 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. marsapp/sqlhelper-yii2

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

marsapp/sqlhelper-yii2
======================

Provide sql helper for Yii2

0.1.0(6y ago)03.1kMITPHP

Since Apr 22Pushed 6y ago1 watchersCompare

[ Source](https://github.com/marshung24/SqlHelper-Yii)[ Packagist](https://packagist.org/packages/marsapp/sqlhelper-yii2)[ Docs](https://github.com/marshung24/SqlHelper-Yii)[ RSS](/packages/marsapp-sqlhelper-yii2/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

SqlHelper for Yii2
==================

[](#sqlhelper-for-yii2)

Provides assistance in using functions to handle SQL grammar construction.

[![Latest Stable Version](https://camo.githubusercontent.com/82cee758c637a65e5ad41c78790b3cf6f614a67e33ee1a83a20c6d9cde85e443/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f73716c68656c7065722f762f737461626c65)](https://packagist.org/packages/marsapp/sqlhelper) [![Total Downloads](https://camo.githubusercontent.com/598ae8b0ad560cf37d45e0b77a28f42552583102e4612070059aa7a04a3c9a1c/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f73716c68656c7065722f646f776e6c6f616473)](https://packagist.org/packages/marsapp/sqlhelper) [![Latest Unstable Version](https://camo.githubusercontent.com/2cf27f423abe25c384bb2570be9e6d564d5fae8ff6ed515486b0c1d5a0db39ed/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f73716c68656c7065722f762f756e737461626c65)](https://packagist.org/packages/marsapp/sqlhelper) [![License](https://camo.githubusercontent.com/2151d40be05da4a7b479bcfb1b7d9476668dd82046ce4c335c669894a13addfb/68747470733a2f2f706f7365722e707567782e6f72672f6d6172736170702f73716c68656c7065722f6c6963656e7365)](https://packagist.org/packages/marsapp/sqlhelper)

- [SqlHelper for Yii2](#sqlhelper-for-yii2)
    - [Installation](#installation)
        - [Composer Install](#composer-install)
    - [API Reference](#api-reference)
        - [batchUpdate](#batchupdate)
            - [Description](#description)
            - [Usage](#usage)
            - [SQL syntax](#sql-syntax)
        - [whereInChunk](#whereinchunk)
            - [Description](#description-1)
            - [Usage](#usage-1)
            - [SQL syntax](#sql-syntax-1)
        - [timeIntersect](#timeintersect)
            - [Description](#description-2)
            - [Usage](#usage-2)
            - [SQL syntax](#sql-syntax-2)

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

[](#installation)

### Composer Install

[](#composer-install)

```
# composer require marsapp/sqlhelper-yii2

```

API Reference
-------------

[](#api-reference)

### batchUpdate

[](#batchupdate)

#### Description

[](#description)

Help construct the query syntax for batch updates by using the functions.

```
batchUpdate($table, $columns, $rows, $conditionArray, $conditionColumn) : \yii\db\Command
```

> Parameters
>
> - $table: table name
> - $columns: Fields to be processed
> - $rows: The values to be processed
> - $conditionArray: The value of the target field
> - $conditionColumn: The name of the target field
>
> Return Values
>
> - Returns \\yii\\db\\Command

#### Usage

[](#usage)

We can use batchUpdate via SqlHelper as follows:

```
// Create SqlHelper Object
$sqlHelper = new \marsapp\helper\sql\SqlHelper();

// Setting batch update data
$query = $sqlHelper->batchUpdate('account', ['c_name', 'age'], [
   ['Mars', 35],
   ['Gunter', 24],
   ['Molly', 25],
 ],[ 1, 2, 3],'id');

// Run batch update
$data = $query->execute();

// Echo last query statement
echo $quiery->getRawSql();
echo "\n";
var_export($data);
```

#### SQL syntax

[](#sql-syntax)

The database syntax constructed by batchUpdate is as follows:

```
UPDATE `account` SET
`c_name` = CASE `id`
     WHEN 1 THEN 'Mars'
     WHEN 2 THEN 'Gunter'
     WHEN 3 THEN 'Molly'
END,
`age` = CASE `id`
     WHEN 1 THEN 35
     WHEN 2 THEN 24
     WHEN 3 THEN 25
END
WHERE `id` IN (1,2,3);
```

### whereInChunk

[](#whereinchunk)

#### Description

[](#description-1)

Help us split the array by using the functions.

```
whereInChunk($fieldName, $fieldList, $query, $size) : ActiveQuery
```

> Parameters
>
> - $fieldName: Field to be processed
> - $fieldList: The values to be processed
> - $query: ActiveQuery
> - $size: Cutting length
>
> Return Values
>
> - Returns ActiveQuery

#### Usage

[](#usage-1)

We can use whereInChunk via SqlHelper as follows:

```
// Setting Arguments
$fieldName = 'pkey';
$fieldList = [1, 2, 3, 4];
$query = ActiveRecord::find();
$size = 3;
$data = \marsapp\helpers\sql\SqlHelper::whereInChunk($fieldName, $fieldList, $query, $size)
    ->asArray()
    ->all();

// Print Query Syntax
echo $query->createCommand()->sql;
echo "\n";
echo $query->createCommand()->getRawSql();
echo "\n";

// Print Data
var_export($data);
```

#### SQL syntax

[](#sql-syntax-1)

The database syntax constructed by batchUpdate is as follows:

```
SELECT * FROM `TABLE_NAME`
WHERE
  (`pkey` IN(1, 2, 3)) OR (`pkey` = 4)

```

### timeIntersect

[](#timeintersect)

#### Description

[](#description-2)

Help construct the query syntax for the intersection of time periods by using the functions.

```
timeIntersect($sCol, $eCol, $sDate, $eDate, $query) : ActiveQuery
```

> Parameters
>
> - $sCol: Field name for start date
> - $eCol: Field namd for end date
> - $sDate: Value for start date
> - $eDate: Value for end date
> - $query: ActiveQuery
>
> Return Values
>
> - Returns ActiveQuery

#### Usage

[](#usage-2)

We can use timeIntersect via SqlHelper as follows:

```
// Setting Arguments
$sCol = 'start_date';
$eCol = 'endd_date';
$sDate = '2020-04-01';
$eDate = '2020-04-30';

// Sql Builder
$query = ActiveRecord::find();
$data = \marsapp\helpers\sql\SqlHelper::timeIntersect($sCol, $eCol, $sDate, $eDate, $query)
    ->asArray()
    ->all();

// Print Query Syntax
echo $query->createCommand()->sql;
echo "\n";
echo $query->createCommand()->getRawSql();
echo "\n";

// Print Data
var_export($data);
```

#### SQL syntax

[](#sql-syntax-2)

The database syntax constructed by batchUpdate is as follows:

```
SELECT * FROM `TABLE_NAME`
WHERE NOT
  (
    (`start_date` > '2020-04-30') OR(
      (`endd_date` < '2020-04-01') AND(`endd_date`  '0000-00-00')
    )
  )

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

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

Unknown

Total

1

Last Release

2210d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/34496839?v=4)[MarsHung](/maintainers/marshung24)[@marshung24](https://github.com/marshung24)

---

Top Contributors

[![marshung24](https://avatars.githubusercontent.com/u/34496839?v=4)](https://github.com/marshung24 "marshung24 (2 commits)")

### Embed Badge

![Health badge](/badges/marsapp-sqlhelper-yii2/health.svg)

```
[![Health](https://phpackages.com/badges/marsapp-sqlhelper-yii2/health.svg)](https://phpackages.com/packages/marsapp-sqlhelper-yii2)
```

###  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)[mongodb/mongodb

MongoDB driver library

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

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/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)
