https://github.com/imliam/yii2-traits
A collection of miscellaneous traits to extend parts of Yii2.
https://github.com/imliam/yii2-traits
Last synced: 6 months ago
JSON representation
A collection of miscellaneous traits to extend parts of Yii2.
- Host: GitHub
- URL: https://github.com/imliam/yii2-traits
- Owner: imliam
- License: mit
- Created: 2018-07-22T16:49:10.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-22T17:01:05.000Z (almost 8 years ago)
- Last Synced: 2025-06-01T12:25:41.407Z (about 1 year ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Yii2 Traits
[](https://packagist.org/packages/imliam/yii2-traits)
[](https://packagist.org/packages/imliam/yii2-traits)
[](LICENSE.md)
A collection of miscellaneous traits to extend parts of Yii2.
- [Yii2 Traits](#yii2-traits)
- [💾 Installation](#💾-installation)
- [📝 Usage](#📝-usage)
- [`yii\db\Migration`](#yii\db\migration)
- [Migration@fillColumn(string $table, string $column, $value)](#migrationfillcolumnstring-table-string-column-value)
- [`yii\db\ActiveRecord`](#yii\db\activerecord)
- [ActiveRecord@firstOrCreate(array $attributes, array $values = []): self](#activerecordfirstorcreatearray-attributes-array-values---self)
- [ActiveRecord@create(array $attributes): self](#activerecordcreatearray-attributes-self)
- [ActiveRecord@make(array $attributes): self](#activerecordmakearray-attributes-self)
- [ActiveRecord@deleteIfExists(array $attributes)](#activerecorddeleteifexistsarray-attributes)
- [ActiveRecord@first(string $orderBy = null)](#activerecordfirststring-orderby--null)
- [✅ Testing](#✅-testing)
- [🔖 Changelog](#🔖-changelog)
- [⬆️ Upgrading](#⬆️-upgrading)
- [🎉 Contributing](#🎉-contributing)
- [🔒 Security](#🔒-security)
- [👷 Credits](#👷-credits)
- [♻️ License](#♻️-license)
## 💾 Installation
You can install the package with [Composer](https://getcomposer.org/) using the following command:
```bash
composer require imliam/yii2-traits:^1.0.0
```
Once installed, you can then `use` the traits in your existing classes.
## 📝 Usage
### `yii\db\Migration`
#### Migration@fillColumn(string $table, string $column, $value)
Set the default value of an existing column.
```php
addColumn('users', 'country', 'string');
$this->fillColumn('users', 'country', 'GB');
}
}
```
### `yii\db\ActiveRecord`
#### ActiveRecord@firstOrCreate(array $attributes, array $values = []): self
Get the first record matching the attributes or create it. Perfect for handling pivot models.
```php
1], ['username' => 'Admin']);
// Returns user ID 1 if it exists in the database, or creates
// a new user with the ID of 1 and username of 'Admin'
```
#### ActiveRecord@create(array $attributes): self
Create a new instance of the model and persist it.
```php
'Admin']);
// Creates and returns a new user with the username of 'Admin'
```
#### ActiveRecord@make(array $attributes): self
Create a new instance of the model without persisting it.
```php
'Admin']);
// Makes a new user with the username of 'Admin' but does NOT save it to the database
```
#### ActiveRecord@deleteIfExists(array $attributes)
Delete a model matching the given attributes.
```php
'Admin']);
// Deletes all users with the username of 'Admin'
```
#### ActiveRecord@first(string $orderBy = null)
Get the first instance of a model.
```php