olegsoft / first-or-create
Trait for Yii2 ActiveRecord. Search for a model ActiveRecord or create a new one in case of failure
Installs: 11 366
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 3
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.4.0
- yiisoft/yii2: 2.0.*
This package is not auto-updated.
Last update: 2024-11-10 06:01:55 UTC
README
The idea is borrowed from Laravel framework.
Installation
The preferred way to install this extension is through composer.
php composer.phar require --prefer-dist olegsoft/first-or-create "dev-master"
Inserting into the ActiveRecord class
use olegsoft\firstOrCreate\FirstOrCreate; class ModelTable extends \yii\db\ActiveRecord { use FirstOrCreate; ...
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 ...