Skip to content
This repository was archived by the owner on Aug 21, 2020. It is now read-only.

ice-leng/yii-db

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Yii2 db


当前版本为测试版本 抽离yii2 的db 库, 使扩展可以集成到任何框架

安装

The preferred way to install this extension is through composer.

Either run

composer require lengbin/yii-db

or add

"lengbin/yii-db": "*"

to the require section of your composer.json file.

Usage

用法和yii 是一致的

方法一 依赖注入

[
    \Lengbin\YiiDb\ConnectionInterface::class => Connection::class,
    // 可以不用实现, 记录sql操作日志
    \Psr\Log\LoggerInterface => Logger::class, 
]

方法二 直接使用

$connect = new \Lengbin\YiiDb\Connection([
    'driver'  => 'mysql',
    'host'    => '127.0.0.1',
    'dbname'  => 'hyperf',
    'charset' => 'utf8',
    'prot'    =>  3306,
]);
$connect->username = 'root';
$connect->password = '';
$connect->tablePrefix = '';

// 方法 一
//$query = new \Lengbin\YiiDb\Query();
//$query->from('demo');
//$data = $query->all($connect);
//$query->where(['id' => 79]);
//$data = $query->one($connect);

// 方法 二
//$query = new \Lengbin\YiiDb\Query($connect);
//$query->from('demo');
//$data = $query->all();
//$query->where(['id' => 79]);
//$data = $query->one();

class Demo extends \Lengbin\YiiDb\ActiveRecord\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'demo';
    }

    public static function getDb()
    {
//        $connect = new \Lengbin\YiiDb\Connection([
//            'driver'  => 'mysql',
//            'host'    => '127.0.0.1',
//            'dbname'  => 'hyperf',
//            'charset' => 'utf8',
//        ]);
//        $connect->username = 'root';
//        $connect->password = '';
//        return $connect;
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['is_delete'], 'default', 'value' => 0],
            [['id', 'is_delete', 'created_at', 'updated_at'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'         => 'ID',
            'name'       => '标题',
            'is_delete'  => '是否删除',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }
}

// select
// 方法 一
//$data = (new Demo())->find()->all($connect);
//$data = Demo::find()->all($connect);

//$data = (new Demo())->find()->where(['id' => 79])->one($connect);
//$data = Demo::find()->where(['id' => 79])->one($connect);

// 方法 二
//$data = (new Demo($connect))->find()->all();
//$data = (new Demo($connect))->find()->where(['id' => 79])->one();

// 方法三
// 需要 实现 getDb 返回 connection 对象
//$data = (new Demo())->find()->all();
//$data = Demo::find()->all();

//$data = (new Demo())->find()->where(['id' => 79])->one();
//$data = Demo::find()->where(['id' => 79])->one();

//insert
// 方法 一
//$model = (new Demo($connect));
//$model->setAttributes(['name' => 'hello2']);
//$data = $model->save();

// 方法 二
// 需要 实现 getDb 返回 connection 对象
//$model = (new Demo());
//$model->setAttributes(['name' => 'hello']);
//$data = $model->save();


//update
// 方法 一
//$model = (new Demo())->find()->where(['id' => 79])->one($connect);;
// 方法 二
//$model = (new Demo($connect))->find()->where(['id' => 79])->one();
// 方法三
// 需要 实现 getDb 返回 connection 对象
//$model = (new Demo())->find()->where(['id' => 79])->one();;

//$model->setAttributes(['name' => 'hello4']);
//$data = $model->save();

// delete
// 方法 一
//$model = (new Demo())->find()->where(['id' => 79])->one($connect);;
// 方法 二
//$model = (new Demo($connect))->find()->where(['id' => 79])->one();
// 方法三
// 需要 实现 getDb 返回 connection 对象
//$model = (new Demo())->find()->where(['id' => 79])->one();;

去掉不必要的

  • behaviors
  • 上传验证
  • 图片验证
  • 文件上传验证
  • 验证码验证

其他

如果出现 minimum-stability冲突 解决方案

有问题请及时联系我,反正也会在使用中修复 - - !

About

yii2的query和activeRecode类抽离出来单独其他框架使用

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages