imliam / yii2-traits
一组用于扩展 Yii2 部分的杂项特性
v1.0.0
2018-07-22 16:58 UTC
Requires
- php: ^7.1
This package is auto-updated.
Last update: 2024-08-29 04:54:23 UTC
README
一组用于扩展 Yii2 部分的杂项特性。
💾 安装
您可以使用以下命令通过 Composer 安装此包
composer require imliam/yii2-traits:^1.0.0
安装完成后,您可以在现有的类中使用这些特性。
📝 使用方法
yii\db\Migration
Migration@fillColumn(string $table, string $column, $value)
设置现有列的默认值。
<?php use yii\db\Migration; use ImLiam\Yii2Traits\MigrationHelpers; class m180524_091606_run_migration extends Migration { use MigrationHelpers; public function safeUp() $this->addColumn('users', 'country', 'string'); $this->fillColumn('users', 'country', 'GB'); } }
yii\db\ActiveRecord
ActiveRecord@firstOrCreate(array $attributes, array $values = []): self
获取匹配属性的第一个记录或创建它。非常适合处理连接模型。
<?php use yii\db\ActiveRecord; use ImLiam\Yii2Traits\ActiveRecordHelpers; class User extends ActiveRecord { use ActiveRecordHelpers; } $user = User::firstOrCreate(['id' => 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
创建模型的新实例并保存。
<?php use yii\db\ActiveRecord; use ImLiam\Yii2Traits\ActiveRecordHelpers; class User extends ActiveRecord { use ActiveRecordHelpers; } $user = User::create(['username' => 'Admin']); // Creates and returns a new user with the username of 'Admin'
ActiveRecord@make(array $attributes): self
创建模型的新实例但不保存。
<?php use yii\db\ActiveRecord; use ImLiam\Yii2Traits\ActiveRecordHelpers; class User extends ActiveRecord { use ActiveRecordHelpers; } $user = User::make(['username' => 'Admin']); // Makes a new user with the username of 'Admin' but does NOT save it to the database
ActiveRecord@deleteIfExists(array $attributes)
删除匹配给定属性的模型。
<?php use yii\db\ActiveRecord; use ImLiam\Yii2Traits\ActiveRecordHelpers; class User extends ActiveRecord { use ActiveRecordHelpers; } User::deleteIfExists(['username' => 'Admin']); // Deletes all users with the username of 'Admin'
ActiveRecord@first(string $orderBy = null)
获取模型的第一实例。
<?php use yii\db\ActiveRecord; use ImLiam\Yii2Traits\ActiveRecordHelpers; class User extends ActiveRecord { use ActiveRecordHelpers; } $user = User::first(); // Returns the first user in the database
✅ 测试
composer test
🔖 更新日志
有关最近更改的更多信息,请参阅 更新日志文件
⬆️ 升级
有关从先前版本升级的详细信息,请参阅 升级文件
🎉 贡献
有关如何为项目做出贡献的详细信息,请参阅 贡献文件 和 行为准则
🔒 安全
如果您发现任何与安全相关的问题,请通过电子邮件 liam@liamhammett.com 而不是使用问题跟踪器联系。
👷 致谢
♻️ 许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件