PHPackages                             olegsoft/first-or-create - 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. olegsoft/first-or-create

ActiveYii2-extension

olegsoft/first-or-create
========================

Trait for Yii2 ActiveRecord. Search for a model ActiveRecord or create a new one in case of failure

416.9k↓35.9%3PHP

Since Jun 15Pushed 7y ago1 watchersCompare

[ Source](https://github.com/oleg-p/first-or-create)[ Packagist](https://packagist.org/packages/olegsoft/first-or-create)[ RSS](/packages/olegsoft-first-or-create/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Trait For Yii2 ActiveRecord
===========================

[](#trait-for-yii2-activerecord)

The idea is borrowed from Laravel framework.
--------------------------------------------

[](#the-idea-is-borrowed-from-laravel-framework)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

```
php composer.phar require --prefer-dist olegsoft/first-or-create "dev-master"
```

Inserting into the ActiveRecord class
-------------------------------------

[](#inserting-into-the-activerecord-class)

```
use olegsoft\firstOrCreate\FirstOrCreate;

class ModelTable extends \yii\db\ActiveRecord
{
    use FirstOrCreate;
    ...
```

Example of use
--------------

[](#example-of-use)

```
use app\models\ModelTable;
    ...

    //public static function firstOrNew($attributes, $values = [])
    $model = ModelTable::firstOrNew(['id' => 50]);
    $model = ModelTable::firstOrNew(['id' => 50], ['sort' => 10]);
    //Returns a single of the ActiveRecord model instance that matches the values of the $attribute array values
    //or returns a new instance of the ActiveRecord model
    //with properties corresponding to the values of the $attributes array + values of the $values array

    //public static function firstOrCreate($attributes, $values = [])
    $model = ModelTable::firstOrCreate(['id' => 50]);
    $model = ModelTable::firstOrCreate(['id' => 50], ['sort' => 10]);
    //Returns a single of the ActiveRecord model instance that matches the values of the $attribute array values
    //or returns a new instance of the ActiveRecord model
    //with properties corresponding to the values of the $attributes array + values of the $values array and save it

    //public static function updateOrCreate($attributes, $values = [])
    $model = ModelTable::updateOrCreate(['id' => 50]);
    $model = ModelTable::updateOrCreate(['id' => 50], ['sort' => 10]);
    //Finds the model from the passed attributes,
    //if the model is found, then assign the values of the $values and save it
    //if the model is not found, then create it with the values $attributes + $value and save it

    //public static function firstOrFail($attributes)
    $model = ModelTable::firstOrFail(['id' => 50]);
    //Return the model with the passed attributes, if the model is not found, then the HTTP 404 exception will be thrown

    //public static function findOrFail($attributes)
    $models = ModelTable::findOrFail(['id' => 50]);
    //Returns array of models by the passed attributes, if no model is found, then the HTTP 404 exception will be thrown

...
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity40

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/4e857a55335d4d21bea61bb659d55fd3fab2bef605a05a0f1de93242fb0d8bcd?d=identicon)[oleg-p](/maintainers/oleg-p)

---

Top Contributors

[![oleg-p](https://avatars.githubusercontent.com/u/7285991?v=4)](https://github.com/oleg-p "oleg-p (17 commits)")

### Embed Badge

![Health badge](/badges/olegsoft-first-or-create/health.svg)

```
[![Health](https://phpackages.com/badges/olegsoft-first-or-create/health.svg)](https://phpackages.com/packages/olegsoft-first-or-create)
```

PHPackages © 2026

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